Harald Welte | c676c68 | 2017-12-24 20:38:20 +0100 | [diff] [blame] | 1 | module IuUP_Emulation { |
| 2 | |
Harald Welte | 35bb716 | 2018-01-03 21:07:52 +0100 | [diff] [blame] | 3 | /* IuUP emulation, uses the encoding/decoding from IuUP_Types. |
| 4 | * |
| 5 | * rather than running in a separate component with related primitives, |
| 6 | * we just implement a set of functions and data types which can be used |
| 7 | * by other code (such as an RTP endpoint) to implement IuUP support. |
| 8 | * |
| 9 | * (C) 2017 by Harald Welte <laforge@gnumonks.org> |
| 10 | * All rights reserved. |
| 11 | * |
| 12 | * Released under the terms of GNU General Public License, Version 2 or |
| 13 | * (at your option) any later version. |
Harald Welte | 34b5a95 | 2019-05-27 11:54:11 +0200 | [diff] [blame] | 14 | * |
| 15 | * SPDX-License-Identifier: GPL-2.0-or-later |
Harald Welte | 35bb716 | 2018-01-03 21:07:52 +0100 | [diff] [blame] | 16 | */ |
| 17 | |
Harald Welte | c676c68 | 2017-12-24 20:38:20 +0100 | [diff] [blame] | 18 | import from Osmocom_Types all; |
| 19 | import from IuUP_Types all; |
| 20 | |
| 21 | |
| 22 | type record IuUP_RabFlowCombination { |
| 23 | IuUP_RFCI rfci, |
| 24 | /* number of bits per sub-flow */ |
| 25 | RecOfU8 sub_flow_bits, |
| 26 | /* IPTI value in number of ITIs for the corresponding RFCI */ |
| 27 | uint8_t ipti |
| 28 | }; |
Pau Espin Pedrol | 6ed7630 | 2022-05-25 18:09:37 +0200 | [diff] [blame] | 29 | type record of IuUP_RabFlowCombination IuUP_RabFlowCombinationList; |
Harald Welte | c676c68 | 2017-12-24 20:38:20 +0100 | [diff] [blame] | 30 | |
| 31 | template (value) IuUP_RabFlowCombination t_IuUP_RFC(IuUP_RFCI rfci, RecOfU8 subflow_bits, uint8_t ipti) := { |
| 32 | rfci := rfci, |
| 33 | sub_flow_bits := subflow_bits, |
| 34 | ipti := ipti |
| 35 | } |
| 36 | |
| 37 | template (value) IuUP_RabFlowCombination t_IuUP_RFC_AMR_12_2(IuUP_RFCI rfci) := t_IuUP_RFC(rfci, {81, 103, 60}, 1); |
| 38 | template (value) IuUP_RabFlowCombination t_IuUP_RFC_AMR_SID(IuUP_RFCI rfci) := t_IuUP_RFC(rfci, {34, 0, 0}, 7); |
Pau Espin Pedrol | 52ee518 | 2022-05-25 17:34:44 +0200 | [diff] [blame] | 39 | template (value) IuUP_RabFlowCombination t_IuUP_RFC_AMR_NO_DATA(IuUP_RFCI rfci) := t_IuUP_RFC(rfci, {0, 0, 0}, 1); |
Harald Welte | c676c68 | 2017-12-24 20:38:20 +0100 | [diff] [blame] | 40 | |
Pau Espin Pedrol | 6ed7630 | 2022-05-25 18:09:37 +0200 | [diff] [blame] | 41 | |
| 42 | const IuUP_RabFlowCombinationList c_IuUP_Config_RabFlowCombination_def := { |
| 43 | { |
| 44 | rfci := 0, |
| 45 | sub_flow_bits := {81, 103, 60}, |
| 46 | ipti := 1 |
| 47 | }, { |
| 48 | rfci := 1, |
| 49 | sub_flow_bits := {34, 0, 0}, |
| 50 | ipti := 7 |
| 51 | }, { |
| 52 | rfci := 2, |
| 53 | sub_flow_bits := {0, 0, 0}, |
| 54 | ipti := 1 |
| 55 | } |
| 56 | }; |
Harald Welte | c676c68 | 2017-12-24 20:38:20 +0100 | [diff] [blame] | 57 | type record IuUP_Config { |
| 58 | /* actively send INIT (true) or only passively respond (false) */ |
| 59 | boolean active_init, |
| 60 | boolean data_pdu_type_0, |
| 61 | /* RAB Flow Combinations */ |
Pau Espin Pedrol | 6ed7630 | 2022-05-25 18:09:37 +0200 | [diff] [blame] | 62 | IuUP_RabFlowCombinationList rab_flow_combs |
Harald Welte | c676c68 | 2017-12-24 20:38:20 +0100 | [diff] [blame] | 63 | }; |
| 64 | |
Pau Espin Pedrol | 6ed7630 | 2022-05-25 18:09:37 +0200 | [diff] [blame] | 65 | template (value) IuUP_Config t_IuUP_Config(boolean active_init := true, |
| 66 | boolean data_pdu_type_0 := true, |
| 67 | template (value) IuUP_RabFlowCombinationList rab_flow_combs := |
| 68 | c_IuUP_Config_RabFlowCombination_def) := { |
| 69 | active_init := active_init, |
| 70 | data_pdu_type_0 := true, |
| 71 | rab_flow_combs := rab_flow_combs |
| 72 | } |
| 73 | |
| 74 | const IuUP_Config c_IuUP_Config_def := { |
| 75 | active_init := true, |
| 76 | data_pdu_type_0 := true, |
| 77 | rab_flow_combs := c_IuUP_Config_RabFlowCombination_def |
| 78 | } |
| 79 | |
Harald Welte | c676c68 | 2017-12-24 20:38:20 +0100 | [diff] [blame] | 80 | type enumerated IuUP_Em_State { |
| 81 | ST_INIT, |
| 82 | ST_DATA_TRANSFER_READY |
| 83 | }; |
| 84 | |
Harald Welte | c676c68 | 2017-12-24 20:38:20 +0100 | [diff] [blame] | 85 | type record IuUP_Entity { |
| 86 | IuUP_Config cfg, |
| 87 | IuUP_Em_State state, |
| 88 | IuUP_FrameNr tx_next_frame_nr, |
| 89 | IuUP_FrameNr rx_last_frame_nr optional, |
| 90 | IuUP_PDU pending_tx_pdu optional |
| 91 | }; |
| 92 | |
Pau Espin Pedrol | 6ed7630 | 2022-05-25 18:09:37 +0200 | [diff] [blame] | 93 | template (value) IuUP_Entity t_IuUP_Entity(template (value) IuUP_Config cfg) := { |
| 94 | cfg := cfg, |
Harald Welte | c676c68 | 2017-12-24 20:38:20 +0100 | [diff] [blame] | 95 | state := ST_INIT, |
| 96 | tx_next_frame_nr := 0, |
| 97 | rx_last_frame_nr := omit, |
| 98 | pending_tx_pdu := omit |
| 99 | } |
| 100 | |
| 101 | |
| 102 | function f_IuUP_Em_rx_decaps(inout IuUP_Entity st, octetstring inp) return octetstring { |
| 103 | var IuUP_PDU pdu := dec_IuUP_PDU(inp); |
| 104 | if (ischosen(pdu.type_0)) { |
| 105 | if (st.cfg.data_pdu_type_0) { |
| 106 | /* FIXME: check header / CRC */ |
| 107 | st.rx_last_frame_nr := pdu.type_0.frame_nr; |
| 108 | return pdu.type_0.payload; |
| 109 | } else { |
| 110 | setverdict(fail, "PDU Type 0 received but 1 configured"); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 111 | mtc.stop; |
Harald Welte | c676c68 | 2017-12-24 20:38:20 +0100 | [diff] [blame] | 112 | } |
| 113 | } else if (ischosen(pdu.type_1)) { |
| 114 | if (st.cfg.data_pdu_type_0 == false) { |
| 115 | /* FIXME: check header / CRC */ |
| 116 | st.rx_last_frame_nr := pdu.type_1.frame_nr; |
| 117 | return pdu.type_1.payload; |
| 118 | } else { |
| 119 | setverdict(fail, "PDU Type 1 received but 0 configured"); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 120 | mtc.stop; |
Harald Welte | c676c68 | 2017-12-24 20:38:20 +0100 | [diff] [blame] | 121 | } |
| 122 | } else if (ischosen(pdu.type_14)) { |
| 123 | if (match(pdu, tr_IuUP_INIT)) { |
| 124 | if (st.cfg.active_init == true) { |
| 125 | setverdict(fail, "INIT received in ACTIVE role"); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 126 | mtc.stop; |
Harald Welte | c676c68 | 2017-12-24 20:38:20 +0100 | [diff] [blame] | 127 | } else { |
| 128 | /* store an INIT_ACK to be transmitted later */ |
| 129 | st.pending_tx_pdu := valueof(ts_IuUP_INIT_ACK(pdu.type_14.frame_nr, |
Pau Espin Pedrol | 5b4f2c6 | 2022-05-25 17:07:17 +0200 | [diff] [blame] | 130 | pdu.type_14.u.proc.hdr.iuup_version)); |
Harald Welte | c676c68 | 2017-12-24 20:38:20 +0100 | [diff] [blame] | 131 | } |
| 132 | } else if (match(pdu, tr_IuUP_INIT_ACK)) { |
| 133 | if (st.cfg.active_init == true) { |
| 134 | log("IuUP INIT_ACK Received"); |
Pau Espin Pedrol | b204a4e | 2021-12-24 14:18:39 +0100 | [diff] [blame] | 135 | st.pending_tx_pdu := omit; /* Drop pending Init retrans */ |
Harald Welte | c676c68 | 2017-12-24 20:38:20 +0100 | [diff] [blame] | 136 | st.state := ST_DATA_TRANSFER_READY; |
| 137 | } else { |
| 138 | setverdict(fail, "INIT_ACK received in PASSIVE role"); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 139 | mtc.stop; |
Harald Welte | c676c68 | 2017-12-24 20:38:20 +0100 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | return ''O; |
| 143 | } else { |
| 144 | setverdict(fail, "Impossible IuUP PDU decoded from ", inp); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 145 | mtc.stop; |
Harald Welte | c676c68 | 2017-12-24 20:38:20 +0100 | [diff] [blame] | 146 | } |
| 147 | self.stop; |
| 148 | } |
| 149 | |
Pau Espin Pedrol | 52ee518 | 2022-05-25 17:34:44 +0200 | [diff] [blame] | 150 | private function f_ts_IuUP_INIT(inout IuUP_Entity st) return IuUP_PDU |
| 151 | { |
| 152 | var IuUP_PDU pdu; |
| 153 | var uint4_t data_pdu_type; |
| 154 | var template (omit) IuUP_InitRfci rfci := omit; |
| 155 | var IuUP_IPTI_List IPTIs := {}; |
| 156 | var uint4_t num_rfci := lengthof(st.cfg.rab_flow_combs); |
| 157 | |
| 158 | if (st.cfg.data_pdu_type_0 == true) { |
| 159 | data_pdu_type := 0; |
| 160 | } else { |
| 161 | data_pdu_type := 1; |
| 162 | } |
| 163 | |
| 164 | /* Build RFCI list: */ |
| 165 | for (var integer remain := num_rfci; remain > 0; remain := remain - 1) { |
| 166 | var IuUP_RabFlowCombination comb := st.cfg.rab_flow_combs[remain - 1]; |
| 167 | var boolean lri := false; |
| 168 | if (remain == num_rfci) { |
| 169 | lri := true; |
| 170 | } |
| 171 | rfci := ts_IuUP_InitRfci(lri, false, comb.rfci, comb.sub_flow_bits, omit, rfci) |
| 172 | } |
| 173 | |
| 174 | /* Build IPTI list: */ |
| 175 | for (var integer i := 0; i < num_rfci; i := i + 1) { |
| 176 | IPTIs := IPTIs & { st.cfg.rab_flow_combs[i].ipti }; |
| 177 | } |
| 178 | |
| 179 | template (value) IuUP_PDU14_ProcSending_INIT tpl := ts_IuUP_PDU14_ProcSending_INIT( |
| 180 | ti := true, |
| 181 | subflows_per_rfci := num_rfci, |
| 182 | chain_ind := false, |
| 183 | rfci := rfci, |
| 184 | IPTIs := IPTIs, |
| 185 | versions_supported := '0000000000000001'B, |
| 186 | data_pdu_type := data_pdu_type |
| 187 | ); |
| 188 | pdu := valueof(ts_IuUP_INIT(tpl)); |
| 189 | return pdu; |
| 190 | } |
| 191 | |
Harald Welte | c676c68 | 2017-12-24 20:38:20 +0100 | [diff] [blame] | 192 | function f_IuUP_Em_tx_encap(inout IuUP_Entity st, in octetstring payload) return octetstring { |
| 193 | var IuUP_PDU pdu; |
| 194 | select (st.state) { |
| 195 | case (ST_INIT) { |
| 196 | if (st.cfg.active_init) { |
Pau Espin Pedrol | b204a4e | 2021-12-24 14:18:39 +0100 | [diff] [blame] | 197 | if (not isvalue(st.pending_tx_pdu)) { |
| 198 | /* send INIT */ |
Pau Espin Pedrol | 52ee518 | 2022-05-25 17:34:44 +0200 | [diff] [blame] | 199 | pdu := f_ts_IuUP_INIT(st); |
Pau Espin Pedrol | b204a4e | 2021-12-24 14:18:39 +0100 | [diff] [blame] | 200 | st.pending_tx_pdu := pdu; |
| 201 | } /* else: wait for INIT-ACK return ''O at the end */ |
| 202 | |
Harald Welte | c676c68 | 2017-12-24 20:38:20 +0100 | [diff] [blame] | 203 | } else { |
| 204 | /* wait for INIT */ |
| 205 | if (isvalue(st.pending_tx_pdu)) { |
| 206 | /* we're waiting to transmit the INIT_ACK in response to an |
| 207 | * init (passive) */ |
| 208 | pdu := st.pending_tx_pdu; |
| 209 | st.pending_tx_pdu := omit; |
| 210 | st.state := ST_DATA_TRANSFER_READY; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | case (ST_DATA_TRANSFER_READY) { |
| 215 | if (st.cfg.data_pdu_type_0) { |
| 216 | pdu := valueof(ts_IuUP_Type0(st.tx_next_frame_nr, 0, payload)); |
| 217 | } else { |
| 218 | pdu := valueof(ts_IuUP_Type1(st.tx_next_frame_nr, 0, payload)); |
| 219 | } |
Pau Espin Pedrol | 64fd02b | 2021-12-21 18:19:42 +0100 | [diff] [blame] | 220 | st.tx_next_frame_nr := (st.tx_next_frame_nr + 1) mod 16; |
Harald Welte | c676c68 | 2017-12-24 20:38:20 +0100 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | if (isvalue(pdu)) { |
| 224 | return f_enc_IuUP_PDU(pdu); |
| 225 | } else { |
| 226 | return ''O; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | |
| 231 | } |