blob: bd717739a2492a1a08a26b081705ad5b8cc351d1 [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>
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010027
28#define LLC_MAX_LEN 1543
29
Jacob Erlbeck1d0a52a2015-06-02 11:28:07 +020030struct BTS;
31struct timeval;
32struct msgb;
33
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010034/**
35 * I represent the LLC data to a MS
36 */
37struct gprs_llc {
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +010038 static bool is_user_data_frame(uint8_t *data, size_t len);
Holger Hans Peter Freytherfce431c2013-11-13 15:17:12 +010039
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010040 void init();
41 void reset();
42 void reset_frame_space();
43
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010044 void put_frame(const uint8_t *data, size_t len);
Jacob Erlbeck612e93e2015-03-20 13:57:27 +010045 void put_dummy_frame(size_t req_len);
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010046 void append_frame(const uint8_t *data, size_t len);
47
48 void consume(size_t len);
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +010049 void consume(uint8_t *data, size_t len);
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010050
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +010051 uint16_t chunk_size() const;
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010052 uint16_t remaining_space() const;
53 uint16_t frame_length() const;
54
55 bool fits_in_current_frame(uint8_t size) const;
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +010056
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010057 uint8_t frame[LLC_MAX_LEN]; /* current DL or UL frame */
Holger Hans Peter Freyther32f9a592013-11-13 17:14:42 +010058 uint16_t m_index; /* current write/read position of frame */
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010059 uint16_t m_length; /* len of current DL LLC_frame, 0 == no frame */
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020060};
Holger Hans Peter Freyther550bb882013-12-04 17:10:54 +010061
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020062/**
63 * I store the LLC frames that come from the SGSN.
64 */
65struct gprs_llc_queue {
66 static void calc_pdu_lifetime(BTS *bts, const uint16_t pdu_delay_csec, struct timeval *tv);
67 static bool is_frame_expired(struct timeval *now, struct timeval *tv);
68 static bool is_user_data_frame(uint8_t *data, size_t len);
69
70 void init();
71
72 void enqueue(struct msgb *llc_msg);
73 struct msgb *dequeue();
74 void clear(BTS *bts);
75 size_t size() const;
Jacob Erlbeck07eb6552015-06-15 11:05:44 +020076 size_t octets() const;
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020077
78private:
Daniel Willmann9c623892013-12-04 18:11:47 +010079 uint32_t m_avg_queue_delay; /* Average delay of data going through the queue */
Holger Hans Peter Freyther550bb882013-12-04 17:10:54 +010080 size_t m_queue_size;
Jacob Erlbeck07eb6552015-06-15 11:05:44 +020081 size_t m_queue_octets;
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020082 struct llist_head m_queue; /* queued LLC DL data */
83
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010084};
85
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020086
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +010087inline uint16_t gprs_llc::chunk_size() const
88{
Holger Hans Peter Freyther32f9a592013-11-13 17:14:42 +010089 return m_length - m_index;
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010090}
91
92inline uint16_t gprs_llc::remaining_space() const
93{
Holger Hans Peter Freyther60582202013-11-21 21:30:23 +010094 return LLC_MAX_LEN - m_length;
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010095}
96
97inline uint16_t gprs_llc::frame_length() const
98{
99 return m_length;
100}
101
102inline void gprs_llc::consume(size_t len)
103{
Holger Hans Peter Freyther32f9a592013-11-13 17:14:42 +0100104 m_index += len;
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +0100105}
106
107inline void gprs_llc::consume(uint8_t *data, size_t len)
108{
109 /* copy and increment index */
Holger Hans Peter Freyther32f9a592013-11-13 17:14:42 +0100110 memcpy(data, frame + m_index, len);
111 consume(len);
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +0100112}
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +0100113
114inline bool gprs_llc::fits_in_current_frame(uint8_t chunk_size) const
115{
Holger Hans Peter Freyther60582202013-11-21 21:30:23 +0100116 return m_length + chunk_size <= LLC_MAX_LEN;
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +0100117}
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +0200118
119inline size_t gprs_llc_queue::size() const
120{
121 return this ? m_queue_size : 0;
122}
Jacob Erlbeck07eb6552015-06-15 11:05:44 +0200123
124inline size_t gprs_llc_queue::octets() const
125{
126 return this ? m_queue_octets : 0;
127}