Holger Hans Peter Freyther | be57081 | 2013-11-07 08:01:49 +0100 | [diff] [blame] | 1 | /* |
| 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 | 4f8384b | 2022-03-31 19:36:12 +0200 | [diff] [blame^] | 26 | #include <osmocom/core/linuxlist.h> |
| 27 | #include <osmocom/core/msgb.h> |
| 28 | |
Holger Hans Peter Freyther | be57081 | 2013-11-07 08:01:49 +0100 | [diff] [blame] | 29 | #define LLC_MAX_LEN 1543 |
| 30 | |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 31 | struct gprs_rlcmac_bts; |
Jacob Erlbeck | 1d0a52a | 2015-06-02 11:28:07 +0200 | [diff] [blame] | 32 | |
Holger Hans Peter Freyther | be57081 | 2013-11-07 08:01:49 +0100 | [diff] [blame] | 33 | /** |
| 34 | * I represent the LLC data to a MS |
| 35 | */ |
| 36 | struct gprs_llc { |
Holger Hans Peter Freyther | be57081 | 2013-11-07 08:01:49 +0100 | [diff] [blame] | 37 | uint8_t frame[LLC_MAX_LEN]; /* current DL or UL frame */ |
Pau Espin Pedrol | 1463383 | 2022-03-31 19:08:07 +0200 | [diff] [blame] | 38 | uint16_t index; /* current write/read position of frame */ |
| 39 | uint16_t length; /* len of current DL LLC_frame, 0 == no frame */ |
Jacob Erlbeck | 6dbe822 | 2015-05-29 10:37:09 +0200 | [diff] [blame] | 40 | }; |
Pau Espin Pedrol | 4f8384b | 2022-03-31 19:36:12 +0200 | [diff] [blame^] | 41 | bool llc_is_user_data_frame(const uint8_t *data, size_t len); |
| 42 | |
| 43 | void llc_init(struct gprs_llc *llc); |
| 44 | void llc_reset(struct gprs_llc *llc); |
| 45 | void llc_reset_frame_space(struct gprs_llc *llc); |
| 46 | |
| 47 | void llc_put_frame(struct gprs_llc *llc, const uint8_t *data, size_t len); |
| 48 | void llc_put_dummy_frame(struct gprs_llc *llc, size_t req_len); |
| 49 | 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] | 50 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 51 | struct MetaInfo { |
| 52 | struct timespec recv_time; |
| 53 | struct timespec expire_time; |
| 54 | }; |
Jacob Erlbeck | 6dbe822 | 2015-05-29 10:37:09 +0200 | [diff] [blame] | 55 | /** |
| 56 | * I store the LLC frames that come from the SGSN. |
| 57 | */ |
| 58 | struct gprs_llc_queue { |
Pau Espin Pedrol | 1463383 | 2022-03-31 19:08:07 +0200 | [diff] [blame] | 59 | uint32_t avg_queue_delay; /* Average delay of data going through the queue */ |
| 60 | size_t queue_size; |
| 61 | size_t queue_octets; |
| 62 | struct llist_head queue; /* queued LLC DL data */ |
Holger Hans Peter Freyther | be57081 | 2013-11-07 08:01:49 +0100 | [diff] [blame] | 63 | }; |
| 64 | |
Pau Espin Pedrol | 4f8384b | 2022-03-31 19:36:12 +0200 | [diff] [blame^] | 65 | void llc_queue_calc_pdu_lifetime(struct gprs_rlcmac_bts *bts, const uint16_t pdu_delay_csec, |
| 66 | struct timespec *tv); |
| 67 | bool llc_queue_is_frame_expired(const struct timespec *tv_now, const struct timespec *tv); |
| 68 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 69 | void llc_queue_init(struct gprs_llc_queue *q); |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 70 | 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] | 71 | void llc_queue_move_and_merge(struct gprs_llc_queue *q, struct gprs_llc_queue *o); |
Pau Espin Pedrol | 4f8384b | 2022-03-31 19:36:12 +0200 | [diff] [blame^] | 72 | void llc_queue_enqueue(struct gprs_llc_queue *q, struct msgb *llc_msg, const struct timespec *expire_time); |
| 73 | struct msgb *llc_queue_dequeue(struct gprs_llc_queue *q, const struct MetaInfo **info); |
Jacob Erlbeck | 6dbe822 | 2015-05-29 10:37:09 +0200 | [diff] [blame] | 74 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 75 | 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] | 76 | { |
Pau Espin Pedrol | 1463383 | 2022-03-31 19:08:07 +0200 | [diff] [blame] | 77 | return llc->length - llc->index; |
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_remaining_space(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_MAX_LEN - 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 uint16_t llc_frame_length(const struct gprs_llc *llc) |
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 | return llc->length; |
Holger Hans Peter Freyther | e231026 | 2013-11-13 16:56:15 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 90 | 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] | 91 | { |
Pau Espin Pedrol | 1463383 | 2022-03-31 19:08:07 +0200 | [diff] [blame] | 92 | llc->index += len; |
Holger Hans Peter Freyther | acb5427 | 2013-11-07 08:15:58 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 95 | 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] | 96 | { |
| 97 | /* copy and increment index */ |
Pau Espin Pedrol | 1463383 | 2022-03-31 19:08:07 +0200 | [diff] [blame] | 98 | memcpy(data, llc->frame + llc->index, len); |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 99 | llc_consume(llc, len); |
Holger Hans Peter Freyther | acb5427 | 2013-11-07 08:15:58 +0100 | [diff] [blame] | 100 | } |
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 | 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] | 103 | { |
Pau Espin Pedrol | 1463383 | 2022-03-31 19:08:07 +0200 | [diff] [blame] | 104 | return llc->length + chunk_size <= LLC_MAX_LEN; |
Holger Hans Peter Freyther | e231026 | 2013-11-13 16:56:15 +0100 | [diff] [blame] | 105 | } |
Jacob Erlbeck | 6dbe822 | 2015-05-29 10:37:09 +0200 | [diff] [blame] | 106 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 107 | 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] | 108 | { |
Pau Espin Pedrol | 1463383 | 2022-03-31 19:08:07 +0200 | [diff] [blame] | 109 | return q->queue_size; |
Jacob Erlbeck | 6dbe822 | 2015-05-29 10:37:09 +0200 | [diff] [blame] | 110 | } |
Jacob Erlbeck | 07eb655 | 2015-06-15 11:05:44 +0200 | [diff] [blame] | 111 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 112 | 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] | 113 | { |
Pau Espin Pedrol | 1463383 | 2022-03-31 19:08:07 +0200 | [diff] [blame] | 114 | return q->queue_octets; |
Jacob Erlbeck | 07eb655 | 2015-06-15 11:05:44 +0200 | [diff] [blame] | 115 | } |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 116 | |
| 117 | #ifdef __cplusplus |
| 118 | } |
| 119 | #endif |