Pau Espin Pedrol | 5c598c7 | 2022-03-31 20:22:05 +0200 | [diff] [blame] | 1 | /* 3GPP TS 44.064 |
Holger Hans Peter Freyther | be57081 | 2013-11-07 08:01:49 +0100 | [diff] [blame] | 2 | * Copyright (C) 2013 by Holger Hans Peter Freyther |
Pau Espin Pedrol | 4f8384b | 2022-03-31 19:36:12 +0200 | [diff] [blame] | 3 | * Copyright (C) 2022 by by Sysmocom s.f.m.c. GmbH |
Holger Hans Peter Freyther | be57081 | 2013-11-07 08:01:49 +0100 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Holger Hans Peter Freyther | be57081 | 2013-11-07 08:01:49 +0100 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #pragma once |
| 17 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 18 | #ifdef __cplusplus |
Jacob Erlbeck | 1eae96c | 2015-06-15 11:19:13 +0200 | [diff] [blame] | 19 | extern "C" { |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 20 | #endif |
Jacob Erlbeck | 1eae96c | 2015-06-15 11:19:13 +0200 | [diff] [blame] | 21 | |
Holger Hans Peter Freyther | be57081 | 2013-11-07 08:01:49 +0100 | [diff] [blame] | 22 | #include <stdint.h> |
Holger Hans Peter Freyther | acb5427 | 2013-11-07 08:15:58 +0100 | [diff] [blame] | 23 | #include <string.h> |
Pau Espin Pedrol | 1de6873 | 2020-03-11 14:04:52 +0100 | [diff] [blame] | 24 | #include <time.h> |
Holger Hans Peter Freyther | be57081 | 2013-11-07 08:01:49 +0100 | [diff] [blame] | 25 | |
Pau Espin Pedrol | 5c598c7 | 2022-03-31 20:22:05 +0200 | [diff] [blame] | 26 | #include <osmocom/core/endian.h> |
Pau Espin Pedrol | 4f8384b | 2022-03-31 19:36:12 +0200 | [diff] [blame] | 27 | #include <osmocom/core/linuxlist.h> |
| 28 | #include <osmocom/core/msgb.h> |
Pau Espin Pedrol | 5c598c7 | 2022-03-31 20:22:05 +0200 | [diff] [blame] | 29 | #include <osmocom/core/endian.h> |
Pau Espin Pedrol | 4f8384b | 2022-03-31 19:36:12 +0200 | [diff] [blame] | 30 | |
Pau Espin Pedrol | 6b1e951 | 2022-04-04 13:45:56 +0200 | [diff] [blame] | 31 | #include "gprs_codel.h" |
| 32 | |
Holger Hans Peter Freyther | be57081 | 2013-11-07 08:01:49 +0100 | [diff] [blame] | 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; |
Pau Espin Pedrol | 6b1e951 | 2022-04-04 13:45:56 +0200 | [diff] [blame] | 36 | struct GprsMs; |
Jacob Erlbeck | 1d0a52a | 2015-06-02 11:28:07 +0200 | [diff] [blame] | 37 | |
Pau Espin Pedrol | 5c598c7 | 2022-03-31 20:22:05 +0200 | [diff] [blame] | 38 | struct gprs_llc_hdr { |
| 39 | #if OSMO_IS_LITTLE_ENDIAN |
| 40 | union { /* 5.2, 6.2.0 */ |
| 41 | uint8_t address; |
| 42 | uint8_t sapi:4, unused:2, c_r:1, pd:1; |
| 43 | #elif OSMO_IS_BIG_ENDIAN |
Oliver Smith | 83ee452 | 2023-02-20 10:50:17 +0100 | [diff] [blame] | 44 | /* auto-generated from the little endian part above (libosmocore/contrib/struct_endianness.py) */ |
Pau Espin Pedrol | 5c598c7 | 2022-03-31 20:22:05 +0200 | [diff] [blame] | 45 | union { |
| 46 | uint8_t address; |
| 47 | uint8_t pd:1, c_r:1, unused:2, sapi:4; |
| 48 | #endif |
| 49 | }; |
| 50 | uint8_t control[0]; |
| 51 | } __attribute__ ((packed)); |
| 52 | |
Holger Hans Peter Freyther | be57081 | 2013-11-07 08:01:49 +0100 | [diff] [blame] | 53 | /** |
| 54 | * I represent the LLC data to a MS |
| 55 | */ |
| 56 | struct gprs_llc { |
Holger Hans Peter Freyther | be57081 | 2013-11-07 08:01:49 +0100 | [diff] [blame] | 57 | uint8_t frame[LLC_MAX_LEN]; /* current DL or UL frame */ |
Pau Espin Pedrol | 1463383 | 2022-03-31 19:08:07 +0200 | [diff] [blame] | 58 | uint16_t index; /* current write/read position of frame */ |
| 59 | uint16_t length; /* len of current DL LLC_frame, 0 == no frame */ |
Jacob Erlbeck | 6dbe822 | 2015-05-29 10:37:09 +0200 | [diff] [blame] | 60 | }; |
Pau Espin Pedrol | 4f8384b | 2022-03-31 19:36:12 +0200 | [diff] [blame] | 61 | |
| 62 | void llc_init(struct gprs_llc *llc); |
| 63 | void llc_reset(struct gprs_llc *llc); |
| 64 | void llc_reset_frame_space(struct gprs_llc *llc); |
| 65 | |
| 66 | void llc_put_frame(struct gprs_llc *llc, const uint8_t *data, size_t len); |
| 67 | void llc_put_dummy_frame(struct gprs_llc *llc, size_t req_len); |
| 68 | void llc_append_frame(struct gprs_llc *llc, const uint8_t *data, size_t len); |
Holger Hans Peter Freyther | 550bb88 | 2013-12-04 17:10:54 +0100 | [diff] [blame] | 69 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 70 | 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] | 71 | { |
Pau Espin Pedrol | 1463383 | 2022-03-31 19:08:07 +0200 | [diff] [blame] | 72 | return llc->length - llc->index; |
Holger Hans Peter Freyther | e231026 | 2013-11-13 16:56:15 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 75 | 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] | 76 | { |
Pau Espin Pedrol | 1463383 | 2022-03-31 19:08:07 +0200 | [diff] [blame] | 77 | return LLC_MAX_LEN - llc->length; |
Holger Hans Peter Freyther | e231026 | 2013-11-13 16:56:15 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 80 | 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] | 81 | { |
Pau Espin Pedrol | 1463383 | 2022-03-31 19:08:07 +0200 | [diff] [blame] | 82 | return llc->length; |
Holger Hans Peter Freyther | e231026 | 2013-11-13 16:56:15 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 85 | 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] | 86 | { |
Pau Espin Pedrol | 1463383 | 2022-03-31 19:08:07 +0200 | [diff] [blame] | 87 | llc->index += len; |
Holger Hans Peter Freyther | acb5427 | 2013-11-07 08:15:58 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 90 | 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] | 91 | { |
| 92 | /* copy and increment index */ |
Pau Espin Pedrol | 1463383 | 2022-03-31 19:08:07 +0200 | [diff] [blame] | 93 | memcpy(data, llc->frame + llc->index, len); |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 94 | llc_consume(llc, len); |
Holger Hans Peter Freyther | acb5427 | 2013-11-07 08:15:58 +0100 | [diff] [blame] | 95 | } |
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 | 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] | 98 | { |
Pau Espin Pedrol | 1463383 | 2022-03-31 19:08:07 +0200 | [diff] [blame] | 99 | return llc->length + chunk_size <= LLC_MAX_LEN; |
Holger Hans Peter Freyther | e231026 | 2013-11-13 16:56:15 +0100 | [diff] [blame] | 100 | } |
Jacob Erlbeck | 6dbe822 | 2015-05-29 10:37:09 +0200 | [diff] [blame] | 101 | |
Pau Espin Pedrol | ee1cb75 | 2022-03-31 19:47:33 +0200 | [diff] [blame] | 102 | struct MetaInfo { |
| 103 | struct timespec recv_time; |
| 104 | struct timespec expire_time; |
| 105 | }; |
| 106 | /** |
| 107 | * I store the LLC frames that come from the SGSN. |
| 108 | */ |
Pau Espin Pedrol | 5c598c7 | 2022-03-31 20:22:05 +0200 | [diff] [blame] | 109 | enum gprs_llc_queue_prio { /* lowest value has highest prio */ |
| 110 | LLC_QUEUE_PRIO_GMM = 0, /* SAPI 1 */ |
| 111 | LLC_QUEUE_PRIO_TOM_SMS, /* SAPI 2,7,8 */ |
| 112 | LLC_QUEUE_PRIO_OTHER, /* Other SAPIs */ |
| 113 | _LLC_QUEUE_PRIO_SIZE /* used to calculate size of enum */ |
| 114 | }; |
Pau Espin Pedrol | 6b1e951 | 2022-04-04 13:45:56 +0200 | [diff] [blame] | 115 | struct gprs_llc_prio_queue { |
| 116 | struct gprs_codel codel_state; |
| 117 | struct llist_head queue; /* queued LLC DL data. See enum gprs_llc_queue_prio. */ |
| 118 | }; |
Pau Espin Pedrol | ee1cb75 | 2022-03-31 19:47:33 +0200 | [diff] [blame] | 119 | struct gprs_llc_queue { |
Pau Espin Pedrol | 6b1e951 | 2022-04-04 13:45:56 +0200 | [diff] [blame] | 120 | struct GprsMs *ms; /* backpointer */ |
Pau Espin Pedrol | ee1cb75 | 2022-03-31 19:47:33 +0200 | [diff] [blame] | 121 | uint32_t avg_queue_delay; /* Average delay of data going through the queue */ |
| 122 | size_t queue_size; |
| 123 | size_t queue_octets; |
Pau Espin Pedrol | 6b1e951 | 2022-04-04 13:45:56 +0200 | [diff] [blame] | 124 | bool use_codel; |
| 125 | struct gprs_llc_prio_queue pq[_LLC_QUEUE_PRIO_SIZE]; /* queued LLC DL data. See enum gprs_llc_queue_prio. */ |
Pau Espin Pedrol | ee1cb75 | 2022-03-31 19:47:33 +0200 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | void llc_queue_calc_pdu_lifetime(struct gprs_rlcmac_bts *bts, const uint16_t pdu_delay_csec, |
| 129 | struct timespec *tv); |
| 130 | bool llc_queue_is_frame_expired(const struct timespec *tv_now, const struct timespec *tv); |
| 131 | |
Pau Espin Pedrol | 6b1e951 | 2022-04-04 13:45:56 +0200 | [diff] [blame] | 132 | void llc_queue_init(struct gprs_llc_queue *q, struct GprsMs *ms); |
Pau Espin Pedrol | ee1cb75 | 2022-03-31 19:47:33 +0200 | [diff] [blame] | 133 | void llc_queue_clear(struct gprs_llc_queue *q, struct gprs_rlcmac_bts *bts); |
Pau Espin Pedrol | 6b1e951 | 2022-04-04 13:45:56 +0200 | [diff] [blame] | 134 | void llc_queue_set_codel_interval(struct gprs_llc_queue *q, unsigned int interval); |
Pau Espin Pedrol | ee1cb75 | 2022-03-31 19:47:33 +0200 | [diff] [blame] | 135 | void llc_queue_move_and_merge(struct gprs_llc_queue *q, struct gprs_llc_queue *o); |
| 136 | void llc_queue_enqueue(struct gprs_llc_queue *q, struct msgb *llc_msg, const struct timespec *expire_time); |
Pau Espin Pedrol | 6b1e951 | 2022-04-04 13:45:56 +0200 | [diff] [blame] | 137 | struct msgb *llc_queue_dequeue(struct gprs_llc_queue *q); |
Pau Espin Pedrol | ee1cb75 | 2022-03-31 19:47:33 +0200 | [diff] [blame] | 138 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 139 | 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] | 140 | { |
Pau Espin Pedrol | 1463383 | 2022-03-31 19:08:07 +0200 | [diff] [blame] | 141 | return q->queue_size; |
Jacob Erlbeck | 6dbe822 | 2015-05-29 10:37:09 +0200 | [diff] [blame] | 142 | } |
Jacob Erlbeck | 07eb655 | 2015-06-15 11:05:44 +0200 | [diff] [blame] | 143 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 144 | 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] | 145 | { |
Pau Espin Pedrol | 1463383 | 2022-03-31 19:08:07 +0200 | [diff] [blame] | 146 | return q->queue_octets; |
Jacob Erlbeck | 07eb655 | 2015-06-15 11:05:44 +0200 | [diff] [blame] | 147 | } |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 148 | |
| 149 | #ifdef __cplusplus |
| 150 | } |
| 151 | #endif |