Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 1 | module NS_Emulation { |
| 2 | import from NS_Types all; |
| 3 | import from BSSGP_Types all; |
Harald Welte | e0abc47 | 2018-02-05 09:13:31 +0100 | [diff] [blame] | 4 | import from Osmocom_Gb_Types all; |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 5 | import from NS_CodecPort all; |
| 6 | import from NS_CodecPort_CtrlFunct all; |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 7 | import from IPL4asp_Types all; |
| 8 | |
| 9 | type record NsUnitdataRequest { |
| 10 | BssgpBvci bvci, |
| 11 | Nsei nsei, |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 12 | octetstring sdu optional, |
Harald Welte | e0abc47 | 2018-02-05 09:13:31 +0100 | [diff] [blame] | 13 | PDU_BSSGP bssgp optional |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 14 | } |
| 15 | |
| 16 | template NsUnitdataRequest t_NsUdReq(template Nsei nsei, template BssgpBvci bvci, template octetstring sdu, |
Harald Welte | e0abc47 | 2018-02-05 09:13:31 +0100 | [diff] [blame] | 17 | template PDU_BSSGP bssgp) := { |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 18 | bvci := bvci, |
| 19 | nsei := nsei, |
| 20 | sdu := sdu, |
| 21 | bssgp := bssgp |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | type record NsUnitdataIndication { |
| 25 | BssgpBvci bvci, |
| 26 | Nsei nsei, |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 27 | octetstring sdu optional, |
Harald Welte | e0abc47 | 2018-02-05 09:13:31 +0100 | [diff] [blame] | 28 | PDU_BSSGP bssgp optional |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | template NsUnitdataIndication t_NsUdInd(Nsei nsei, BssgpBvci bvci, octetstring sdu) := { |
| 32 | bvci := bvci, |
| 33 | nsei := nsei, |
| 34 | sdu := sdu, |
Harald Welte | e0abc47 | 2018-02-05 09:13:31 +0100 | [diff] [blame] | 35 | bssgp := dec_PDU_BSSGP(sdu) |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | type record NsStatusIndication { |
| 39 | Nsei nsei, |
| 40 | Nsvci nsvci, |
| 41 | NseState old_state, |
| 42 | NseState new_state |
| 43 | } |
| 44 | |
| 45 | template NsStatusIndication t_NsStsInd(Nsei nsei, Nsvci nsvci, NseState old_state, NseState state) := { |
| 46 | nsei := nsei, |
| 47 | nsvci := nsvci, |
| 48 | old_state := old_state, |
| 49 | new_state := state |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | type enumerated NseState { |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 53 | NSE_S_DEAD_BLOCKED, |
| 54 | NSE_S_WAIT_RESET, |
| 55 | NSE_S_ALIVE_BLOCKED, |
| 56 | NSE_S_ALIVE_UNBLOCKED |
| 57 | } |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 58 | |
| 59 | /* port from our (internal) point of view */ |
| 60 | type port NS_SP_PT message { |
| 61 | in NsUnitdataRequest; |
| 62 | out NsUnitdataIndication, |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 63 | NsStatusIndication, |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 64 | ASP_Event; |
| 65 | } with { extension "internal" }; |
| 66 | |
| 67 | /* port from the user point of view */ |
| 68 | type port NS_PT message { |
| 69 | in ASP_Event, |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 70 | NsStatusIndication, |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 71 | NsUnitdataIndication; |
| 72 | out NsUnitdataRequest; |
| 73 | } with { extension "internal" }; |
| 74 | |
Alexander Couzens | 2c12b24 | 2018-07-31 00:30:11 +0200 | [diff] [blame] | 75 | function NSStart(NSConfiguration init_config) runs on NS_CT { |
| 76 | config := init_config; |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 77 | f_init(); |
| 78 | f_ScanEvents(); |
| 79 | } |
| 80 | |
| 81 | private function f_init() runs on NS_CT { |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 82 | var Result res; |
| 83 | /* Connect the UDP socket */ |
Alexander Couzens | 2c12b24 | 2018-07-31 00:30:11 +0200 | [diff] [blame] | 84 | res := f_IPL4_connect(NSCP, config.remote_ip, config.remote_udp_port, config.local_ip, config.local_udp_port, 0, { udp := {}}); |
Harald Welte | 9220f63 | 2018-05-23 20:27:02 +0200 | [diff] [blame] | 85 | if (not ispresent(res.connId)) { |
| 86 | setverdict(fail, "Could not connect NS UDP socket, check your configuration"); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 87 | mtc.stop; |
Harald Welte | 9220f63 | 2018-05-23 20:27:02 +0200 | [diff] [blame] | 88 | } |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 89 | g_conn_id := res.connId; |
| 90 | f_change_state(NSE_S_DEAD_BLOCKED); |
| 91 | /* Send the first NS-ALIVE to test the connection */ |
| 92 | f_sendReset(); |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | type component NS_CT { |
| 96 | /* UDP port towards the bottom (IUT) */ |
| 97 | port NS_CODEC_PT NSCP; |
| 98 | /* NS-User SAP towards the user */ |
| 99 | port NS_SP_PT NS_SP; |
| 100 | |
Alexander Couzens | 2c12b24 | 2018-07-31 00:30:11 +0200 | [diff] [blame] | 101 | var NSConfiguration config; |
| 102 | |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 103 | var NseState g_state := NSE_S_DEAD_BLOCKED; |
| 104 | var ConnectionId g_conn_id := -1; |
| 105 | |
| 106 | timer Tns_alive := 3.0; |
| 107 | timer Tns_test := 10.0; |
| 108 | timer Tns_block := 10.0; |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Alexander Couzens | 2c12b24 | 2018-07-31 00:30:11 +0200 | [diff] [blame] | 111 | type record NSConfiguration { |
| 112 | PortNumber local_udp_port, |
| 113 | charstring local_ip, |
| 114 | PortNumber remote_udp_port, |
| 115 | charstring remote_ip, |
| 116 | Nsvci nsvci, |
| 117 | Nsvci nsei |
| 118 | } |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 119 | |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 120 | private function f_change_state(NseState new_state) runs on NS_CT { |
| 121 | var NseState old_state := g_state; |
| 122 | g_state := new_state; |
| 123 | log("NS State Transition: ", old_state, " -> ", new_state); |
Alexander Couzens | 2c12b24 | 2018-07-31 00:30:11 +0200 | [diff] [blame] | 124 | NS_SP.send(t_NsStsInd(config.nsei, config.nsvci, old_state, new_state)); |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 127 | private function f_sendReset() runs on NS_CT { |
Alexander Couzens | 2c12b24 | 2018-07-31 00:30:11 +0200 | [diff] [blame] | 128 | NSCP.send(t_NS_Send(g_conn_id, t_NS_RESET(NS_CAUSE_OM_INTERVENTION, config.nsvci, config.nsei))); |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 129 | g_state := NSE_S_WAIT_RESET; |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 132 | private function f_sendAlive() runs on NS_CT { |
| 133 | NSCP.send(t_NS_Send(g_conn_id, t_NS_ALIVE)); |
| 134 | Tns_alive.start; |
| 135 | } |
| 136 | |
| 137 | private function f_sendUnblock() runs on NS_CT { |
| 138 | NSCP.send(t_NS_Send(g_conn_id, t_NS_UNBLOCK)); |
| 139 | Tns_block.start; |
| 140 | } |
| 141 | |
| 142 | private function f_sendBlock(NsCause cause) runs on NS_CT { |
Alexander Couzens | 2c12b24 | 2018-07-31 00:30:11 +0200 | [diff] [blame] | 143 | NSCP.send(t_NS_Send(g_conn_id, t_NS_BLOCK(cause, config.nsvci))); |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 144 | Tns_block.start; |
| 145 | } |
| 146 | |
| 147 | altstep as_allstate() runs on NS_CT { |
| 148 | var NS_RecvFrom rf; |
| 149 | var ASP_Event evt; |
| 150 | |
| 151 | /* transition to DEAD if t_alive times out */ |
Harald Welte | c27f684 | 2018-03-02 21:40:03 +0100 | [diff] [blame] | 152 | [Tns_alive.running] Tns_alive.timeout { |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 153 | log("Tns-alive expired: changing to DEAD_BLOCKED + starting Tns-test"); |
| 154 | f_change_state(NSE_S_DEAD_BLOCKED); |
| 155 | Tns_test.start; |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 156 | } |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 157 | |
Harald Welte | c27f684 | 2018-03-02 21:40:03 +0100 | [diff] [blame] | 158 | [Tns_test.running] Tns_test.timeout { |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 159 | log("Tns-test expired: sending NS-ALIVE"); |
| 160 | f_sendAlive(); |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 161 | } |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 162 | |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 163 | /* Stop t_alive when receiving ALIVE-ACK */ |
| 164 | [] NSCP.receive(t_NS_RecvFrom(t_NS_ALIVE_ACK)) { |
| 165 | log("NS-ALIVE-ACK received: stopping Tns-alive; starting Tns-test"); |
| 166 | Tns_alive.stop; |
| 167 | Tns_test.start; |
| 168 | } |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 169 | |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 170 | /* respond to NS-ALIVE with NS-ALIVE-ACK */ |
| 171 | [] NSCP.receive(t_NS_RecvFrom(t_NS_ALIVE)) { |
| 172 | NSCP.send(t_NS_Send(g_conn_id, t_NS_ALIVE_ACK)); |
| 173 | } |
| 174 | |
| 175 | /* Respond to BLOCK for wrong NSVCI */ |
| 176 | [] NSCP.receive(t_NS_RecvFrom(t_NS_BLOCK(?, ?))) -> value rf { |
| 177 | log("Rx NS-BLOCK for unknown NSVCI"); |
| 178 | /* FIXME */ |
| 179 | } |
| 180 | |
| 181 | /* Respond to RESET with correct NSEI/NSVCI */ |
Alexander Couzens | 2c12b24 | 2018-07-31 00:30:11 +0200 | [diff] [blame] | 182 | [] NSCP.receive(t_NS_RecvFrom(t_NS_RESET(?, config.nsvci, config.nsei))) -> value rf { |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 183 | f_change_state(NSE_S_ALIVE_BLOCKED); |
Alexander Couzens | 2c12b24 | 2018-07-31 00:30:11 +0200 | [diff] [blame] | 184 | NSCP.send(t_NS_Send(g_conn_id, t_NS_RESET_ACK(config.nsvci, config.nsei))); |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /* Respond to RESET with wrong NSEI/NSVCI */ |
| 188 | [] NSCP.receive(t_NS_RecvFrom(t_NS_RESET(?, ?, ?))) -> value rf { |
| 189 | log("Rx NS-RESET for unknown NSEI/NSVCI"); |
| 190 | /* FIXME */ |
| 191 | } |
| 192 | |
| 193 | /* default case of handling unknown PDUs */ |
| 194 | [] NSCP.receive(t_NS_RecvFrom(?)) -> value rf { |
| 195 | log("Rx Unexpected NS PDU ", rf.msg," in state ", g_state); |
Harald Welte | e0abc47 | 2018-02-05 09:13:31 +0100 | [diff] [blame] | 196 | NSCP.send(t_NS_Send(g_conn_id, ts_NS_STATUS(NS_CAUSE_PDU_NOT_COMPATIBLE_WITH_PROTOCOL_STATE, rf.msg))); |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 197 | } |
| 198 | /* Forwarding of ASP_Evet to user */ |
| 199 | [] NSCP.receive(ASP_Event:?) -> value evt { NS_SP.send(evt); } |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | private function f_ScanEvents() runs on NS_CT { |
| 203 | var NsUnitdataRequest ud_req; |
| 204 | var NS_RecvFrom rf; |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 205 | var default d; |
| 206 | |
| 207 | d := activate(as_allstate()); |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 208 | |
| 209 | while (true) { |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 210 | if (g_state == NSE_S_DEAD_BLOCKED) { |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 211 | alt { |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 212 | [false] any timer.timeout {} |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 213 | } |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 214 | } else if (g_state == NSE_S_WAIT_RESET) { |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 215 | alt { |
Alexander Couzens | 2c12b24 | 2018-07-31 00:30:11 +0200 | [diff] [blame] | 216 | [] NSCP.receive(t_NS_RecvFrom(t_NS_RESET_ACK(config.nsvci, config.nsei))) -> value rf { |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 217 | f_change_state(NSE_S_ALIVE_BLOCKED); |
| 218 | f_sendAlive(); |
| 219 | f_sendUnblock(); |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 220 | } |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 221 | } |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 222 | } else if (g_state == NSE_S_ALIVE_BLOCKED) { |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 223 | alt { |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 224 | /* bogus block, just respond with ACK */ |
Alexander Couzens | 2c12b24 | 2018-07-31 00:30:11 +0200 | [diff] [blame] | 225 | [] NSCP.receive(t_NS_RecvFrom(t_NS_BLOCK(?, config.nsvci))) -> value rf { |
| 226 | NSCP.send(t_NS_Send(g_conn_id, t_NS_BLOCK_ACK(config.nsvci))); |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 227 | } |
| 228 | /* Respond to UNBLOCK with UNBLOCK-ACK + change state */ |
| 229 | [] NSCP.receive(t_NS_RecvFrom(t_NS_UNBLOCK)) -> value rf { |
| 230 | NSCP.send(t_NS_Send(g_conn_id, t_NS_UNBLOCK_ACK)); |
| 231 | Tns_block.stop; |
| 232 | f_change_state(NSE_S_ALIVE_UNBLOCKED); |
| 233 | } |
| 234 | [] NSCP.receive(t_NS_RecvFrom(t_NS_UNBLOCK_ACK)) -> value rf { |
| 235 | Tns_block.stop; |
| 236 | f_change_state(NSE_S_ALIVE_UNBLOCKED); |
| 237 | } |
| 238 | [] Tns_block.timeout { |
| 239 | /* repeat unblock transmission */ |
| 240 | f_sendUnblock(); |
| 241 | } |
| 242 | } |
| 243 | } else if (g_state == NSE_S_ALIVE_UNBLOCKED) { |
| 244 | alt { |
| 245 | /* bogus unblock, just respond with ACK */ |
| 246 | [] NSCP.receive(t_NS_RecvFrom(t_NS_UNBLOCK)) -> value rf { |
| 247 | NSCP.send(t_NS_Send(g_conn_id, t_NS_UNBLOCK_ACK)); |
| 248 | } |
| 249 | /* Respond to BLOCK with BLOCK-ACK + change state */ |
Alexander Couzens | 2c12b24 | 2018-07-31 00:30:11 +0200 | [diff] [blame] | 250 | [] NSCP.receive(t_NS_RecvFrom(t_NS_BLOCK(?, config.nsvci))) -> value rf { |
| 251 | NSCP.send(t_NS_Send(g_conn_id, t_NS_BLOCK_ACK(config.nsvci))); |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 252 | Tns_block.stop; |
| 253 | f_change_state(NSE_S_ALIVE_BLOCKED); |
| 254 | } |
Alexander Couzens | 2c12b24 | 2018-07-31 00:30:11 +0200 | [diff] [blame] | 255 | [] NSCP.receive(t_NS_RecvFrom(t_NS_BLOCK_ACK(config.nsvci))) -> value rf { |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 256 | Tns_block.stop; |
| 257 | } |
| 258 | /* NS-UNITDATA PDU from network to NS-UNITDATA.ind to user */ |
| 259 | [] NSCP.receive(t_NS_RecvFrom(t_NS_UNITDATA(?, ?, ?))) -> value rf { |
Alexander Couzens | 2c12b24 | 2018-07-31 00:30:11 +0200 | [diff] [blame] | 260 | NS_SP.send(t_NsUdInd(config.nsei, |
Harald Welte | e0abc47 | 2018-02-05 09:13:31 +0100 | [diff] [blame] | 261 | oct2int(rf.msg.pDU_NS_Unitdata.bVCI), |
| 262 | rf.msg.pDU_NS_Unitdata.nS_SDU)); |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 263 | } |
| 264 | /* NS-UNITDATA.req from user to NS-UNITDATA PDU on network */ |
Alexander Couzens | 2c12b24 | 2018-07-31 00:30:11 +0200 | [diff] [blame] | 265 | [] NS_SP.receive(t_NsUdReq(config.nsei, ?, ?, omit)) -> value ud_req { |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 266 | /* using raw octetstring PDU */ |
| 267 | NSCP.send(t_NS_Send(g_conn_id, t_NS_UNITDATA(t_SduCtrlB, ud_req.bvci, ud_req.sdu))); |
| 268 | } |
Alexander Couzens | 2c12b24 | 2018-07-31 00:30:11 +0200 | [diff] [blame] | 269 | [] NS_SP.receive(t_NsUdReq(config.nsei, ?, omit, ?)) -> value ud_req { |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 270 | /* using decoded BSSGP PDU that we need to encode first */ |
Harald Welte | e0abc47 | 2018-02-05 09:13:31 +0100 | [diff] [blame] | 271 | var octetstring enc := enc_PDU_BSSGP(ud_req.bssgp); |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 272 | NSCP.send(t_NS_Send(g_conn_id, t_NS_UNITDATA(t_SduCtrlB, ud_req.bvci, enc))); |
| 273 | } |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 277 | } /* while */ |
| 278 | //deactivate(d); |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 279 | } |
| 280 | } |