Holger Hans Peter Freyther | be57081 | 2013-11-07 08:01:49 +0100 | [diff] [blame] | 1 | /* |
| 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 Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 21 | #ifdef __cplusplus |
Jacob Erlbeck | 1eae96c | 2015-06-15 11:19:13 +0200 | [diff] [blame] | 22 | extern "C" { |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 23 | #endif |
Jacob Erlbeck | 1eae96c | 2015-06-15 11:19:13 +0200 | [diff] [blame] | 24 | #include <osmocom/core/linuxlist.h> |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 25 | #ifdef __cplusplus |
Jacob Erlbeck | 1eae96c | 2015-06-15 11:19:13 +0200 | [diff] [blame] | 26 | } |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 27 | #endif |
Jacob Erlbeck | 1eae96c | 2015-06-15 11:19:13 +0200 | [diff] [blame] | 28 | |
Holger Hans Peter Freyther | be57081 | 2013-11-07 08:01:49 +0100 | [diff] [blame] | 29 | #include <stdint.h> |
Holger Hans Peter Freyther | acb5427 | 2013-11-07 08:15:58 +0100 | [diff] [blame] | 30 | #include <string.h> |
Pau Espin Pedrol | 1de6873 | 2020-03-11 14:04:52 +0100 | [diff] [blame] | 31 | #include <time.h> |
Holger Hans Peter Freyther | be57081 | 2013-11-07 08:01:49 +0100 | [diff] [blame] | 32 | |
| 33 | #define LLC_MAX_LEN 1543 |
| 34 | |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 35 | struct gprs_rlcmac_bts; |
Jacob Erlbeck | 1d0a52a | 2015-06-02 11:28:07 +0200 | [diff] [blame] | 36 | |
Holger Hans Peter Freyther | be57081 | 2013-11-07 08:01:49 +0100 | [diff] [blame] | 37 | /** |
| 38 | * I represent the LLC data to a MS |
| 39 | */ |
| 40 | struct gprs_llc { |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 41 | |
| 42 | #ifdef __cplusplus |
Jacob Erlbeck | 0c1c877 | 2015-03-20 12:02:42 +0100 | [diff] [blame] | 43 | static bool is_user_data_frame(uint8_t *data, size_t len); |
Holger Hans Peter Freyther | fce431c | 2013-11-13 15:17:12 +0100 | [diff] [blame] | 44 | |
Holger Hans Peter Freyther | be57081 | 2013-11-07 08:01:49 +0100 | [diff] [blame] | 45 | void init(); |
| 46 | void reset(); |
| 47 | void reset_frame_space(); |
| 48 | |
Holger Hans Peter Freyther | be57081 | 2013-11-07 08:01:49 +0100 | [diff] [blame] | 49 | void put_frame(const uint8_t *data, size_t len); |
Jacob Erlbeck | 612e93e | 2015-03-20 13:57:27 +0100 | [diff] [blame] | 50 | void put_dummy_frame(size_t req_len); |
Holger Hans Peter Freyther | e231026 | 2013-11-13 16:56:15 +0100 | [diff] [blame] | 51 | void append_frame(const uint8_t *data, size_t len); |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 52 | #endif |
Holger Hans Peter Freyther | acb5427 | 2013-11-07 08:15:58 +0100 | [diff] [blame] | 53 | |
Holger Hans Peter Freyther | be57081 | 2013-11-07 08:01:49 +0100 | [diff] [blame] | 54 | uint8_t frame[LLC_MAX_LEN]; /* current DL or UL frame */ |
Holger Hans Peter Freyther | 32f9a59 | 2013-11-13 17:14:42 +0100 | [diff] [blame] | 55 | uint16_t m_index; /* current write/read position of frame */ |
Holger Hans Peter Freyther | e231026 | 2013-11-13 16:56:15 +0100 | [diff] [blame] | 56 | uint16_t m_length; /* len of current DL LLC_frame, 0 == no frame */ |
Jacob Erlbeck | 6dbe822 | 2015-05-29 10:37:09 +0200 | [diff] [blame] | 57 | }; |
Holger Hans Peter Freyther | 550bb88 | 2013-12-04 17:10:54 +0100 | [diff] [blame] | 58 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 59 | struct MetaInfo { |
| 60 | struct timespec recv_time; |
| 61 | struct timespec expire_time; |
| 62 | }; |
Jacob Erlbeck | 6dbe822 | 2015-05-29 10:37:09 +0200 | [diff] [blame] | 63 | /** |
| 64 | * I store the LLC frames that come from the SGSN. |
| 65 | */ |
| 66 | struct gprs_llc_queue { |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 67 | #ifdef __cplusplus |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 68 | static void calc_pdu_lifetime(struct gprs_rlcmac_bts *bts, const uint16_t pdu_delay_csec, |
Pau Espin Pedrol | 1de6873 | 2020-03-11 14:04:52 +0100 | [diff] [blame] | 69 | struct timespec *tv); |
| 70 | static bool is_frame_expired(const struct timespec *now, |
| 71 | const struct timespec *tv); |
Jacob Erlbeck | 6dbe822 | 2015-05-29 10:37:09 +0200 | [diff] [blame] | 72 | static bool is_user_data_frame(uint8_t *data, size_t len); |
| 73 | |
Pau Espin Pedrol | 1de6873 | 2020-03-11 14:04:52 +0100 | [diff] [blame] | 74 | void enqueue(struct msgb *llc_msg, const struct timespec *expire_time); |
Jacob Erlbeck | b671dbf | 2015-06-15 14:32:33 +0200 | [diff] [blame] | 75 | struct msgb *dequeue(const MetaInfo **info = 0); |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 76 | #endif |
Daniel Willmann | 9c62389 | 2013-12-04 18:11:47 +0100 | [diff] [blame] | 77 | uint32_t m_avg_queue_delay; /* Average delay of data going through the queue */ |
Holger Hans Peter Freyther | 550bb88 | 2013-12-04 17:10:54 +0100 | [diff] [blame] | 78 | size_t m_queue_size; |
Jacob Erlbeck | 07eb655 | 2015-06-15 11:05:44 +0200 | [diff] [blame] | 79 | size_t m_queue_octets; |
Jacob Erlbeck | 6dbe822 | 2015-05-29 10:37:09 +0200 | [diff] [blame] | 80 | struct llist_head m_queue; /* queued LLC DL data */ |
Holger Hans Peter Freyther | be57081 | 2013-11-07 08:01:49 +0100 | [diff] [blame] | 81 | }; |
| 82 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 83 | #ifdef __cplusplus |
| 84 | extern "C" { |
| 85 | #endif |
| 86 | void llc_queue_init(struct gprs_llc_queue *q); |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 87 | void llc_queue_clear(struct gprs_llc_queue *q, struct gprs_rlcmac_bts *bts); |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 88 | void llc_queue_move_and_merge(struct gprs_llc_queue *q, struct gprs_llc_queue *o); |
Jacob Erlbeck | 6dbe822 | 2015-05-29 10:37:09 +0200 | [diff] [blame] | 89 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 90 | static inline uint16_t llc_chunk_size(const struct gprs_llc *llc) |
Holger Hans Peter Freyther | acb5427 | 2013-11-07 08:15:58 +0100 | [diff] [blame] | 91 | { |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 92 | return llc->m_length - llc->m_index; |
Holger Hans Peter Freyther | e231026 | 2013-11-13 16:56:15 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 95 | static inline uint16_t llc_remaining_space(const struct gprs_llc *llc) |
Holger Hans Peter Freyther | e231026 | 2013-11-13 16:56:15 +0100 | [diff] [blame] | 96 | { |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 97 | return LLC_MAX_LEN - llc->m_length; |
Holger Hans Peter Freyther | e231026 | 2013-11-13 16:56:15 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 100 | static inline uint16_t llc_frame_length(const struct gprs_llc *llc) |
Holger Hans Peter Freyther | e231026 | 2013-11-13 16:56:15 +0100 | [diff] [blame] | 101 | { |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 102 | return llc->m_length; |
Holger Hans Peter Freyther | e231026 | 2013-11-13 16:56:15 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 105 | static inline void llc_consume(struct gprs_llc *llc, size_t len) |
Holger Hans Peter Freyther | e231026 | 2013-11-13 16:56:15 +0100 | [diff] [blame] | 106 | { |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 107 | llc->m_index += len; |
Holger Hans Peter Freyther | acb5427 | 2013-11-07 08:15:58 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 110 | static inline void llc_consume_data(struct gprs_llc *llc, uint8_t *data, size_t len) |
Holger Hans Peter Freyther | acb5427 | 2013-11-07 08:15:58 +0100 | [diff] [blame] | 111 | { |
| 112 | /* copy and increment index */ |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 113 | memcpy(data, llc->frame + llc->m_index, len); |
| 114 | llc_consume(llc, len); |
Holger Hans Peter Freyther | acb5427 | 2013-11-07 08:15:58 +0100 | [diff] [blame] | 115 | } |
Holger Hans Peter Freyther | e231026 | 2013-11-13 16:56:15 +0100 | [diff] [blame] | 116 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 117 | static inline bool llc_fits_in_current_frame(const struct gprs_llc *llc, uint8_t chunk_size) |
Holger Hans Peter Freyther | e231026 | 2013-11-13 16:56:15 +0100 | [diff] [blame] | 118 | { |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 119 | return llc->m_length + chunk_size <= LLC_MAX_LEN; |
Holger Hans Peter Freyther | e231026 | 2013-11-13 16:56:15 +0100 | [diff] [blame] | 120 | } |
Jacob Erlbeck | 6dbe822 | 2015-05-29 10:37:09 +0200 | [diff] [blame] | 121 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 122 | static inline size_t llc_queue_size(const struct gprs_llc_queue *q) |
Jacob Erlbeck | 6dbe822 | 2015-05-29 10:37:09 +0200 | [diff] [blame] | 123 | { |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 124 | return q->m_queue_size; |
Jacob Erlbeck | 6dbe822 | 2015-05-29 10:37:09 +0200 | [diff] [blame] | 125 | } |
Jacob Erlbeck | 07eb655 | 2015-06-15 11:05:44 +0200 | [diff] [blame] | 126 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 127 | static inline size_t llc_queue_octets(const struct gprs_llc_queue *q) |
Jacob Erlbeck | 07eb655 | 2015-06-15 11:05:44 +0200 | [diff] [blame] | 128 | { |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 129 | return q->m_queue_octets; |
Jacob Erlbeck | 07eb655 | 2015-06-15 11:05:44 +0200 | [diff] [blame] | 130 | } |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 131 | |
| 132 | #ifdef __cplusplus |
| 133 | } |
| 134 | #endif |