blob: 623ae4959f705a149c8a5f0a54f8ccb5ee1f3de5 [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.
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010013 */
14
15#pragma once
16
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010017#ifdef __cplusplus
Jacob Erlbeck1eae96c2015-06-15 11:19:13 +020018extern "C" {
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010019#endif
Jacob Erlbeck1eae96c2015-06-15 11:19:13 +020020 #include <osmocom/core/linuxlist.h>
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010021#ifdef __cplusplus
Jacob Erlbeck1eae96c2015-06-15 11:19:13 +020022}
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010023#endif
Jacob Erlbeck1eae96c2015-06-15 11:19:13 +020024
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>
Pau Espin Pedrol1de68732020-03-11 14:04:52 +010027#include <time.h>
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010028
29#define LLC_MAX_LEN 1543
30
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010031struct gprs_rlcmac_bts;
Jacob Erlbeck1d0a52a2015-06-02 11:28:07 +020032
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010033/**
34 * I represent the LLC data to a MS
35 */
36struct gprs_llc {
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010037
38#ifdef __cplusplus
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);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010048#endif
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +010049
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010050 uint8_t frame[LLC_MAX_LEN]; /* current DL or UL frame */
Holger Hans Peter Freyther32f9a592013-11-13 17:14:42 +010051 uint16_t m_index; /* current write/read position of frame */
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010052 uint16_t m_length; /* len of current DL LLC_frame, 0 == no frame */
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020053};
Holger Hans Peter Freyther550bb882013-12-04 17:10:54 +010054
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010055struct MetaInfo {
56 struct timespec recv_time;
57 struct timespec expire_time;
58};
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020059/**
60 * I store the LLC frames that come from the SGSN.
61 */
62struct gprs_llc_queue {
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010063#ifdef __cplusplus
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010064 static void calc_pdu_lifetime(struct gprs_rlcmac_bts *bts, const uint16_t pdu_delay_csec,
Pau Espin Pedrol1de68732020-03-11 14:04:52 +010065 struct timespec *tv);
66 static bool is_frame_expired(const struct timespec *now,
67 const struct timespec *tv);
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020068 static bool is_user_data_frame(uint8_t *data, size_t len);
69
Pau Espin Pedrol1de68732020-03-11 14:04:52 +010070 void enqueue(struct msgb *llc_msg, const struct timespec *expire_time);
Jacob Erlbeckb671dbf2015-06-15 14:32:33 +020071 struct msgb *dequeue(const MetaInfo **info = 0);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010072#endif
Daniel Willmann9c623892013-12-04 18:11:47 +010073 uint32_t m_avg_queue_delay; /* Average delay of data going through the queue */
Holger Hans Peter Freyther550bb882013-12-04 17:10:54 +010074 size_t m_queue_size;
Jacob Erlbeck07eb6552015-06-15 11:05:44 +020075 size_t m_queue_octets;
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020076 struct llist_head m_queue; /* queued LLC DL data */
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010077};
78
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010079#ifdef __cplusplus
80extern "C" {
81#endif
82void llc_queue_init(struct gprs_llc_queue *q);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010083void llc_queue_clear(struct gprs_llc_queue *q, struct gprs_rlcmac_bts *bts);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010084void llc_queue_move_and_merge(struct gprs_llc_queue *q, struct gprs_llc_queue *o);
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020085
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010086static inline uint16_t llc_chunk_size(const struct gprs_llc *llc)
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +010087{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010088 return llc->m_length - llc->m_index;
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010089}
90
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010091static inline uint16_t llc_remaining_space(const struct gprs_llc *llc)
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010092{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010093 return LLC_MAX_LEN - llc->m_length;
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010094}
95
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010096static inline uint16_t llc_frame_length(const struct gprs_llc *llc)
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010097{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010098 return llc->m_length;
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010099}
100
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100101static inline void llc_consume(struct gprs_llc *llc, size_t len)
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +0100102{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100103 llc->m_index += len;
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +0100104}
105
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100106static inline void llc_consume_data(struct gprs_llc *llc, uint8_t *data, size_t len)
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +0100107{
108 /* copy and increment index */
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100109 memcpy(data, llc->frame + llc->m_index, len);
110 llc_consume(llc, len);
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +0100111}
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +0100112
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100113static 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 +0100114{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100115 return llc->m_length + chunk_size <= LLC_MAX_LEN;
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +0100116}
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +0200117
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100118static inline size_t llc_queue_size(const struct gprs_llc_queue *q)
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +0200119{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100120 return q->m_queue_size;
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +0200121}
Jacob Erlbeck07eb6552015-06-15 11:05:44 +0200122
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100123static inline size_t llc_queue_octets(const struct gprs_llc_queue *q)
Jacob Erlbeck07eb6552015-06-15 11:05:44 +0200124{
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100125 return q->m_queue_octets;
Jacob Erlbeck07eb6552015-06-15 11:05:44 +0200126}
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100127
128#ifdef __cplusplus
129}
130#endif