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 | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 4 | import from BSSGP_Helper_Functions 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, |
| 13 | BssgpPdu bssgp optional |
| 14 | } |
| 15 | |
| 16 | template NsUnitdataRequest t_NsUdReq(template Nsei nsei, template BssgpBvci bvci, template octetstring sdu, |
| 17 | template BssgpPdu bssgp) := { |
| 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, |
| 28 | BssgpPdu bssgp optional |
| 29 | } |
| 30 | |
| 31 | template NsUnitdataIndication t_NsUdInd(Nsei nsei, BssgpBvci bvci, octetstring sdu) := { |
| 32 | bvci := bvci, |
| 33 | nsei := nsei, |
| 34 | sdu := sdu, |
| 35 | bssgp := dec_BssgpPdu(f_BSSGP_expand_len(sdu)) |
| 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 | |
| 75 | function NSStart() runs on NS_CT { |
| 76 | f_init(); |
| 77 | f_ScanEvents(); |
| 78 | } |
| 79 | |
| 80 | private function f_init() runs on NS_CT { |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 81 | var Result res; |
| 82 | /* Connect the UDP socket */ |
| 83 | res := f_IPL4_connect(NSCP, mp_remote_ip, mp_remote_udp_port, mp_local_ip, mp_local_udp_port, 0, { udp := {}}); |
| 84 | g_conn_id := res.connId; |
| 85 | f_change_state(NSE_S_DEAD_BLOCKED); |
| 86 | /* Send the first NS-ALIVE to test the connection */ |
| 87 | f_sendReset(); |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | type component NS_CT { |
| 91 | /* UDP port towards the bottom (IUT) */ |
| 92 | port NS_CODEC_PT NSCP; |
| 93 | /* NS-User SAP towards the user */ |
| 94 | port NS_SP_PT NS_SP; |
| 95 | |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 96 | var NseState g_state := NSE_S_DEAD_BLOCKED; |
| 97 | var ConnectionId g_conn_id := -1; |
| 98 | |
| 99 | timer Tns_alive := 3.0; |
| 100 | timer Tns_test := 10.0; |
| 101 | timer Tns_block := 10.0; |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | modulepar { |
Harald Welte | 244cd8a | 2017-08-26 09:25:20 +0200 | [diff] [blame] | 105 | PortNumber mp_local_udp_port := 23000; |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 106 | charstring mp_local_ip := "127.0.0.1"; |
Harald Welte | 244cd8a | 2017-08-26 09:25:20 +0200 | [diff] [blame] | 107 | PortNumber mp_remote_udp_port := 21000; |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 108 | charstring mp_remote_ip := "127.0.0.1"; |
| 109 | Nsvci mp_nsvci := 0; |
| 110 | Nsvci mp_nsei := 2342; |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 111 | }; |
| 112 | |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 113 | private function f_change_state(NseState new_state) runs on NS_CT { |
| 114 | var NseState old_state := g_state; |
| 115 | g_state := new_state; |
| 116 | log("NS State Transition: ", old_state, " -> ", new_state); |
| 117 | NS_SP.send(t_NsStsInd(mp_nsei, mp_nsvci, old_state, new_state)); |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 118 | } |
| 119 | |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 120 | private function f_sendReset() runs on NS_CT { |
| 121 | NSCP.send(t_NS_Send(g_conn_id, t_NS_RESET(NS_CAUSE_OM_INTERVENTION, mp_nsvci, mp_nsei))); |
| 122 | g_state := NSE_S_WAIT_RESET; |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 125 | private function f_sendAlive() runs on NS_CT { |
| 126 | NSCP.send(t_NS_Send(g_conn_id, t_NS_ALIVE)); |
| 127 | Tns_alive.start; |
| 128 | } |
| 129 | |
| 130 | private function f_sendUnblock() runs on NS_CT { |
| 131 | NSCP.send(t_NS_Send(g_conn_id, t_NS_UNBLOCK)); |
| 132 | Tns_block.start; |
| 133 | } |
| 134 | |
| 135 | private function f_sendBlock(NsCause cause) runs on NS_CT { |
| 136 | NSCP.send(t_NS_Send(g_conn_id, t_NS_BLOCK(cause, mp_nsvci))); |
| 137 | Tns_block.start; |
| 138 | } |
| 139 | |
| 140 | altstep as_allstate() runs on NS_CT { |
| 141 | var NS_RecvFrom rf; |
| 142 | var ASP_Event evt; |
| 143 | |
| 144 | /* transition to DEAD if t_alive times out */ |
| 145 | [] Tns_alive.timeout { |
| 146 | log("Tns-alive expired: changing to DEAD_BLOCKED + starting Tns-test"); |
| 147 | f_change_state(NSE_S_DEAD_BLOCKED); |
| 148 | Tns_test.start; |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 149 | } |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 150 | |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 151 | [] Tns_test.timeout { |
| 152 | log("Tns-test expired: sending NS-ALIVE"); |
| 153 | f_sendAlive(); |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 154 | } |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 155 | |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 156 | /* Stop t_alive when receiving ALIVE-ACK */ |
| 157 | [] NSCP.receive(t_NS_RecvFrom(t_NS_ALIVE_ACK)) { |
| 158 | log("NS-ALIVE-ACK received: stopping Tns-alive; starting Tns-test"); |
| 159 | Tns_alive.stop; |
| 160 | Tns_test.start; |
| 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 | /* respond to NS-ALIVE with NS-ALIVE-ACK */ |
| 164 | [] NSCP.receive(t_NS_RecvFrom(t_NS_ALIVE)) { |
| 165 | NSCP.send(t_NS_Send(g_conn_id, t_NS_ALIVE_ACK)); |
| 166 | } |
| 167 | |
| 168 | /* Respond to BLOCK for wrong NSVCI */ |
| 169 | [] NSCP.receive(t_NS_RecvFrom(t_NS_BLOCK(?, ?))) -> value rf { |
| 170 | log("Rx NS-BLOCK for unknown NSVCI"); |
| 171 | /* FIXME */ |
| 172 | } |
| 173 | |
| 174 | /* Respond to RESET with correct NSEI/NSVCI */ |
| 175 | [] NSCP.receive(t_NS_RecvFrom(t_NS_RESET(?, mp_nsvci, mp_nsei))) -> value rf { |
| 176 | f_change_state(NSE_S_ALIVE_BLOCKED); |
| 177 | NSCP.send(t_NS_Send(g_conn_id, t_NS_RESET_ACK(mp_nsvci, mp_nsei))); |
| 178 | } |
| 179 | |
| 180 | /* Respond to RESET with wrong NSEI/NSVCI */ |
| 181 | [] NSCP.receive(t_NS_RecvFrom(t_NS_RESET(?, ?, ?))) -> value rf { |
| 182 | log("Rx NS-RESET for unknown NSEI/NSVCI"); |
| 183 | /* FIXME */ |
| 184 | } |
| 185 | |
| 186 | /* default case of handling unknown PDUs */ |
| 187 | [] NSCP.receive(t_NS_RecvFrom(?)) -> value rf { |
| 188 | log("Rx Unexpected NS PDU ", rf.msg," in state ", g_state); |
| 189 | NSCP.send(t_NS_Send(g_conn_id, t_NS_STATUS(NS_CAUSE_PDU_NOT_COMPATIBLE_WITH_PROTOCOL_STATE, rf.msg))); |
| 190 | } |
| 191 | /* Forwarding of ASP_Evet to user */ |
| 192 | [] NSCP.receive(ASP_Event:?) -> value evt { NS_SP.send(evt); } |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | private function f_ScanEvents() runs on NS_CT { |
| 196 | var NsUnitdataRequest ud_req; |
| 197 | var NS_RecvFrom rf; |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 198 | var default d; |
| 199 | |
| 200 | d := activate(as_allstate()); |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 201 | |
| 202 | while (true) { |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 203 | if (g_state == NSE_S_DEAD_BLOCKED) { |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 204 | alt { |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 205 | [false] any timer.timeout {} |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 206 | } |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 207 | } else if (g_state == NSE_S_WAIT_RESET) { |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 208 | alt { |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 209 | [] NSCP.receive(t_NS_RecvFrom(t_NS_RESET_ACK(mp_nsvci, mp_nsei))) -> value rf { |
| 210 | f_change_state(NSE_S_ALIVE_BLOCKED); |
| 211 | f_sendAlive(); |
| 212 | f_sendUnblock(); |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 213 | } |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 214 | } |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 215 | } else if (g_state == NSE_S_ALIVE_BLOCKED) { |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 216 | alt { |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 217 | /* bogus block, just respond with ACK */ |
| 218 | [] NSCP.receive(t_NS_RecvFrom(t_NS_BLOCK(?, mp_nsvci))) -> value rf { |
| 219 | NSCP.send(t_NS_Send(g_conn_id, t_NS_BLOCK_ACK(mp_nsvci))); |
| 220 | } |
| 221 | /* Respond to UNBLOCK with UNBLOCK-ACK + change state */ |
| 222 | [] NSCP.receive(t_NS_RecvFrom(t_NS_UNBLOCK)) -> value rf { |
| 223 | NSCP.send(t_NS_Send(g_conn_id, t_NS_UNBLOCK_ACK)); |
| 224 | Tns_block.stop; |
| 225 | f_change_state(NSE_S_ALIVE_UNBLOCKED); |
| 226 | } |
| 227 | [] NSCP.receive(t_NS_RecvFrom(t_NS_UNBLOCK_ACK)) -> value rf { |
| 228 | Tns_block.stop; |
| 229 | f_change_state(NSE_S_ALIVE_UNBLOCKED); |
| 230 | } |
| 231 | [] Tns_block.timeout { |
| 232 | /* repeat unblock transmission */ |
| 233 | f_sendUnblock(); |
| 234 | } |
| 235 | } |
| 236 | } else if (g_state == NSE_S_ALIVE_UNBLOCKED) { |
| 237 | alt { |
| 238 | /* bogus unblock, just respond with ACK */ |
| 239 | [] NSCP.receive(t_NS_RecvFrom(t_NS_UNBLOCK)) -> value rf { |
| 240 | NSCP.send(t_NS_Send(g_conn_id, t_NS_UNBLOCK_ACK)); |
| 241 | } |
| 242 | /* Respond to BLOCK with BLOCK-ACK + change state */ |
| 243 | [] NSCP.receive(t_NS_RecvFrom(t_NS_BLOCK(?, mp_nsvci))) -> value rf { |
| 244 | NSCP.send(t_NS_Send(g_conn_id, t_NS_BLOCK_ACK(mp_nsvci))); |
| 245 | Tns_block.stop; |
| 246 | f_change_state(NSE_S_ALIVE_BLOCKED); |
| 247 | } |
| 248 | [] NSCP.receive(t_NS_RecvFrom(t_NS_BLOCK_ACK(mp_nsvci))) -> value rf { |
| 249 | Tns_block.stop; |
| 250 | } |
| 251 | /* NS-UNITDATA PDU from network to NS-UNITDATA.ind to user */ |
| 252 | [] NSCP.receive(t_NS_RecvFrom(t_NS_UNITDATA(?, ?, ?))) -> value rf { |
| 253 | NS_SP.send(t_NsUdInd(mp_nsei, rf.msg.u.unitdata.bvci, rf.msg.u.unitdata.sdu)); |
| 254 | } |
| 255 | /* NS-UNITDATA.req from user to NS-UNITDATA PDU on network */ |
| 256 | [] NS_SP.receive(t_NsUdReq(mp_nsei, ?, ?, omit)) -> value ud_req { |
| 257 | /* using raw octetstring PDU */ |
| 258 | NSCP.send(t_NS_Send(g_conn_id, t_NS_UNITDATA(t_SduCtrlB, ud_req.bvci, ud_req.sdu))); |
| 259 | } |
| 260 | [] NS_SP.receive(t_NsUdReq(mp_nsei, ?, omit, ?)) -> value ud_req { |
| 261 | /* using decoded BSSGP PDU that we need to encode first */ |
| 262 | var octetstring enc := f_BSSGP_compact_len(enc_BssgpPdu(ud_req.bssgp)); |
| 263 | NSCP.send(t_NS_Send(g_conn_id, t_NS_UNITDATA(t_SduCtrlB, ud_req.bvci, enc))); |
| 264 | } |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
Harald Welte | 6e594f2 | 2017-07-23 16:19:35 +0200 | [diff] [blame] | 268 | } /* while */ |
| 269 | //deactivate(d); |
Harald Welte | 6fff364 | 2017-07-22 21:36:13 +0200 | [diff] [blame] | 270 | } |
| 271 | } |