blob: 13662d839f5110b27de4c6ebd9aa7748744eab58 [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
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010021#ifdef __cplusplus
Jacob Erlbeck1eae96c2015-06-15 11:19:13 +020022extern "C" {
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010023#endif
Jacob Erlbeck1eae96c2015-06-15 11:19:13 +020024 #include <osmocom/core/linuxlist.h>
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010025#ifdef __cplusplus
Jacob Erlbeck1eae96c2015-06-15 11:19:13 +020026}
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010027#endif
Jacob Erlbeck1eae96c2015-06-15 11:19:13 +020028
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010029#include <stdint.h>
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +010030#include <string.h>
Pau Espin Pedrol1de68732020-03-11 14:04:52 +010031#include <time.h>
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010032
33#define LLC_MAX_LEN 1543
34
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010035struct gprs_rlcmac_bts;
Jacob Erlbeck1d0a52a2015-06-02 11:28:07 +020036
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010037/**
38 * I represent the LLC data to a MS
39 */
40struct gprs_llc {
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010041
42#ifdef __cplusplus
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +010043 static bool is_user_data_frame(uint8_t *data, size_t len);
Holger Hans Peter Freytherfce431c2013-11-13 15:17:12 +010044
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010045 void init();
46 void reset();
47 void reset_frame_space();
48
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010049 void put_frame(const uint8_t *data, size_t len);
Jacob Erlbeck612e93e2015-03-20 13:57:27 +010050 void put_dummy_frame(size_t req_len);
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010051 void append_frame(const uint8_t *data, size_t len);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010052#endif
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +010053
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010054 uint8_t frame[LLC_MAX_LEN]; /* current DL or UL frame */
Holger Hans Peter Freyther32f9a592013-11-13 17:14:42 +010055 uint16_t m_index; /* current write/read position of frame */
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010056 uint16_t m_length; /* len of current DL LLC_frame, 0 == no frame */
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020057};
Holger Hans Peter Freyther550bb882013-12-04 17:10:54 +010058
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010059struct MetaInfo {
60 struct timespec recv_time;
61 struct timespec expire_time;
62};
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020063/**
64 * I store the LLC frames that come from the SGSN.
65 */
66struct gprs_llc_queue {
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010067#ifdef __cplusplus
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010068 static void calc_pdu_lifetime(struct gprs_rlcmac_bts *bts, const uint16_t pdu_delay_csec,
Pau Espin Pedrol1de68732020-03-11 14:04:52 +010069 struct timespec *tv);
70 static bool is_frame_expired(const struct timespec *now,
71 const struct timespec *tv);
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020072 static bool is_user_data_frame(uint8_t *data, size_t len);
73
Pau Espin Pedrol1de68732020-03-11 14:04:52 +010074 void enqueue(struct msgb *llc_msg, const struct timespec *expire_time);
Jacob Erlbeckb671dbf2015-06-15 14:32:33 +020075 struct msgb *dequeue(const MetaInfo **info = 0);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010076#endif
Daniel Willmann9c623892013-12-04 18:11:47 +010077 uint32_t m_avg_queue_delay; /* Average delay of data going through the queue */
Holger Hans Peter Freyther550bb882013-12-04 17:10:54 +010078 size_t m_queue_size;
Jacob Erlbeck07eb6552015-06-15 11:05:44 +020079 size_t m_queue_octets;
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020080 struct llist_head m_queue; /* queued LLC DL data */
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010081};
82
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010083#ifdef __cplusplus
84extern "C" {
85#endif
86void llc_queue_init(struct gprs_llc_queue *q);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010087void llc_queue_clear(struct gprs_llc_queue *q, struct gprs_rlcmac_bts *bts);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010088void llc_queue_move_and_merge(struct gprs_llc_queue *q, struct gprs_llc_queue *o);
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020089
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010090static inline uint16_t llc_chunk_size(const struct gprs_llc *llc)
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +010091{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010092 return llc->m_length - llc->m_index;
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010093}
94
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010095static inline uint16_t llc_remaining_space(const struct gprs_llc *llc)
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010096{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010097 return LLC_MAX_LEN - llc->m_length;
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010098}
99
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100100static inline uint16_t llc_frame_length(const struct gprs_llc *llc)
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +0100101{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100102 return llc->m_length;
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +0100103}
104
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100105static inline void llc_consume(struct gprs_llc *llc, size_t len)
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +0100106{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100107 llc->m_index += len;
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +0100108}
109
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100110static inline void llc_consume_data(struct gprs_llc *llc, uint8_t *data, size_t len)
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +0100111{
112 /* copy and increment index */
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100113 memcpy(data, llc->frame + llc->m_index, len);
114 llc_consume(llc, len);
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +0100115}
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +0100116
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100117static inline bool llc_fits_in_current_frame(const struct gprs_llc *llc, uint8_t chunk_size)
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +0100118{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100119 return llc->m_length + chunk_size <= LLC_MAX_LEN;
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +0100120}
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +0200121
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100122static inline size_t llc_queue_size(const struct gprs_llc_queue *q)
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +0200123{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100124 return q->m_queue_size;
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +0200125}
Jacob Erlbeck07eb6552015-06-15 11:05:44 +0200126
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100127static inline size_t llc_queue_octets(const struct gprs_llc_queue *q)
Jacob Erlbeck07eb6552015-06-15 11:05:44 +0200128{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100129 return q->m_queue_octets;
Jacob Erlbeck07eb6552015-06-15 11:05:44 +0200130}
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100131
132#ifdef __cplusplus
133}
134#endif