blob: 1b9b1df18a2d6efab8dd72131e06a0f52bc6e651 [file] [log] [blame]
Harald Welte34b5a952019-05-27 11:54:11 +02001/* GPRS-NS Emulation in TTCN-3
Harald Weltebe7afce2021-01-17 22:04:36 +01002 * (C) 2018-2021 Harald Welte <laforge@gnumonks.org>
Harald Welte34b5a952019-05-27 11:54:11 +02003 * contributions by sysmocom - s.f.m.c. GmbH
4 * All rights reserved.
5 *
6 * Released under the terms of GNU General Public License, Version 2 or
7 * (at your option) any later version.
8 *
9 * SPDX-License-Identifier: GPL-2.0-or-later
10 */
11
Harald Welte6fff3642017-07-22 21:36:13 +020012module NS_Emulation {
Harald Welte90f19742020-11-06 19:34:40 +010013 import from Misc_Helpers all;
Harald Welte6fff3642017-07-22 21:36:13 +020014 import from NS_Types all;
15 import from BSSGP_Types all;
Harald Welte867243a2020-09-13 18:32:32 +020016 import from Osmocom_Types all;
Harald Weltee0abc472018-02-05 09:13:31 +010017 import from Osmocom_Gb_Types all;
Harald Welte013d65a2020-09-13 14:41:31 +020018 import from NS_Provider_IPL4 all;
Harald Welte867243a2020-09-13 18:32:32 +020019#ifdef NS_EMULATION_FR
20 import from NS_Provider_FR all;
21#endif
Harald Welte6fff3642017-07-22 21:36:13 +020022 import from IPL4asp_Types all;
23
24 type record NsUnitdataRequest {
25 BssgpBvci bvci,
26 Nsei nsei,
Harald Weltec4505522020-11-11 18:55:09 +010027 integer lsp,
Harald Welte6e594f22017-07-23 16:19:35 +020028 octetstring sdu optional,
Harald Weltee0abc472018-02-05 09:13:31 +010029 PDU_BSSGP bssgp optional
Harald Welte6e594f22017-07-23 16:19:35 +020030 }
31
Harald Weltec4505522020-11-11 18:55:09 +010032 template NsUnitdataRequest tr_NsUdReq(template Nsei nsei, template BssgpBvci bvci, template integer lsp,
33 template octetstring sdu, template PDU_BSSGP bssgp) := {
Harald Welte90f19742020-11-06 19:34:40 +010034 bvci := bvci,
35 nsei := nsei,
Harald Weltec4505522020-11-11 18:55:09 +010036 lsp := lsp,
Harald Welte90f19742020-11-06 19:34:40 +010037 sdu := sdu,
38 bssgp := bssgp
39 }
40
41 template (value) NsUnitdataRequest ts_NsUdReq(template (value) Nsei nsei,
42 template (value) BssgpBvci bvci,
Harald Weltec4505522020-11-11 18:55:09 +010043 template (value) integer lsp,
Harald Welte90f19742020-11-06 19:34:40 +010044 template (omit) octetstring sdu,
45 template (omit) PDU_BSSGP bssgp) := {
Harald Welte6e594f22017-07-23 16:19:35 +020046 bvci := bvci,
47 nsei := nsei,
Harald Weltec4505522020-11-11 18:55:09 +010048 lsp := lsp,
Harald Welte6e594f22017-07-23 16:19:35 +020049 sdu := sdu,
50 bssgp := bssgp
Harald Welte6fff3642017-07-22 21:36:13 +020051 }
52
53 type record NsUnitdataIndication {
54 BssgpBvci bvci,
55 Nsei nsei,
Harald Welte80a249a2020-11-17 19:57:40 +010056 Nsvci nsvci,
Harald Welte6e594f22017-07-23 16:19:35 +020057 octetstring sdu optional,
Harald Weltee0abc472018-02-05 09:13:31 +010058 PDU_BSSGP bssgp optional
Harald Welte6e594f22017-07-23 16:19:35 +020059 }
60
Harald Welte90f19742020-11-06 19:34:40 +010061 template (present) NsUnitdataIndication tr_NsUdInd(template (present) Nsei nsei,
62 template (present) BssgpBvci bvci,
63 template octetstring sdu) := {
64 bvci := bvci,
65 nsei := nsei,
Harald Welte80a249a2020-11-17 19:57:40 +010066 nsvci := ?,
Harald Welte90f19742020-11-06 19:34:40 +010067 sdu := sdu,
68 bssgp := ?
69 }
70
Harald Welte80a249a2020-11-17 19:57:40 +010071 template (value) NsUnitdataIndication ts_NsUdInd(Nsei nsei, Nsvci nsvci, BssgpBvci bvci, octetstring sdu) := {
Harald Welte6e594f22017-07-23 16:19:35 +020072 bvci := bvci,
73 nsei := nsei,
Harald Welte80a249a2020-11-17 19:57:40 +010074 nsvci := nsvci,
Harald Welte6e594f22017-07-23 16:19:35 +020075 sdu := sdu,
Harald Weltee0abc472018-02-05 09:13:31 +010076 bssgp := dec_PDU_BSSGP(sdu)
Harald Welte6e594f22017-07-23 16:19:35 +020077 }
78
79 type record NsStatusIndication {
80 Nsei nsei,
81 Nsvci nsvci,
Harald Welte90f19742020-11-06 19:34:40 +010082 NsvcState old_state,
Harald Weltedc0a0902020-11-10 21:03:29 +010083 NsvcState new_state,
84 boolean first_or_last
Harald Welte6e594f22017-07-23 16:19:35 +020085 }
86
Harald Welte90f19742020-11-06 19:34:40 +010087 template (present) NsStatusIndication tr_NsStsInd(template (present) Nsei nsei := ?,
88 template (present) Nsvci nsvci := ?,
89 template (present) NsvcState old_state := ?,
Harald Weltedc0a0902020-11-10 21:03:29 +010090 template (present) NsvcState state := ?,
91 template (present) boolean first_or_last := ?) := {
Harald Welte6e594f22017-07-23 16:19:35 +020092 nsei := nsei,
93 nsvci := nsvci,
94 old_state := old_state,
Harald Weltedc0a0902020-11-10 21:03:29 +010095 new_state := state,
96 first_or_last := first_or_last
Harald Welte6fff3642017-07-22 21:36:13 +020097 }
98
Harald Welte90f19742020-11-06 19:34:40 +010099
Harald Weltedc0a0902020-11-10 21:03:29 +0100100 template (value) NsStatusIndication ts_NsStsInd(Nsei nsei, Nsvci nsvci, NsvcState old_state, NsvcState state,
101 boolean first_or_last := false) := {
Harald Welte90f19742020-11-06 19:34:40 +0100102 nsei := nsei,
103 nsvci := nsvci,
104 old_state := old_state,
Harald Weltedc0a0902020-11-10 21:03:29 +0100105 new_state := state,
106 first_or_last := first_or_last
Harald Welte90f19742020-11-06 19:34:40 +0100107 }
108
109 type enumerated NsvcState {
Harald Welte3f820892021-01-19 18:19:23 +0100110 NSVC_S_DISABLED, /* administratively disabled */
Harald Welte90f19742020-11-06 19:34:40 +0100111 NSVC_S_DEAD_BLOCKED,
112 NSVC_S_WAIT_RESET,
113 NSVC_S_ALIVE_BLOCKED,
114 NSVC_S_ALIVE_UNBLOCKED
Harald Welte6e594f22017-07-23 16:19:35 +0200115 }
Harald Welte6fff3642017-07-22 21:36:13 +0200116
117 /* port from our (internal) point of view */
118 type port NS_SP_PT message {
119 in NsUnitdataRequest;
120 out NsUnitdataIndication,
Harald Welte013d65a2020-09-13 14:41:31 +0200121 NsStatusIndication;
Harald Welte6fff3642017-07-22 21:36:13 +0200122 } with { extension "internal" };
123
124 /* port from the user point of view */
125 type port NS_PT message {
126 in ASP_Event,
Harald Welte6e594f22017-07-23 16:19:35 +0200127 NsStatusIndication,
Harald Welte6fff3642017-07-22 21:36:13 +0200128 NsUnitdataIndication;
129 out NsUnitdataRequest;
130 } with { extension "internal" };
131
Harald Welte3f820892021-01-19 18:19:23 +0100132 /* port from our (internal) point of view */
133 type port NS_CTRL_SP_PT message {
134 in NsDisableVcRequest,
135 NsEnableVcRequest;
136 } with { extension "internal" };
137
138 /* port from the user point of view */
139 type port NS_CTRL_PT message {
140 out NsEnableVcRequest,
141 NsDisableVcRequest;
142 } with { extension "internal" };
143
144 type record NsDisableVcRequest {
145 Nsvci nsvci
146 };
147 type record NsEnableVcRequest {
148 Nsvci nsvci
149 };
150
Harald Welte013d65a2020-09-13 14:41:31 +0200151 type component NS_Provider_CT {
152 /* upper port, facing to NS_Emulation:NSCP */
153 port NS_PROVIDER_PT NSE;
154 /* lower layer ports (UDP/IP, Frame Relay) are added in derived components */
155 };
156
Harald Weltebd612cd2020-09-14 09:42:28 +0200157 type enumerated NS_Provider_LinkStatus {
158 NS_PROV_LINK_STATUS_UP,
159 NS_PROV_LINK_STATUS_DOWN
160 };
161 type union NS_Provider_Evt {
162 NS_Provider_LinkStatus link_status
163 };
164
Harald Welte013d65a2020-09-13 14:41:31 +0200165 /* port between NS_Provider and NS_CT */
166 type port NS_PROVIDER_PT message {
Harald Weltebd612cd2020-09-14 09:42:28 +0200167 inout PDU_NS, NS_Provider_Evt;
Harald Welte013d65a2020-09-13 14:41:31 +0200168 } with { extension "internal" };
169
Harald Welte90f19742020-11-06 19:34:40 +0100170 type record NSVCConfigurationIP {
171 AddressFamily address_family,
172 PortNumber local_udp_port,
173 charstring local_ip,
174 PortNumber remote_udp_port,
Harald Weltebe7afce2021-01-17 22:04:36 +0100175 charstring remote_ip,
176 uint8_t data_weight,
177 uint8_t signalling_weight
Harald Welte90f19742020-11-06 19:34:40 +0100178 };
179 type record NSVCConfigurationFR {
180 charstring netdev, /* HDLC net-device for AF_PACKET socket */
181 integer dlci
182 };
183 type union NSVCConfigurationP {
184 NSVCConfigurationIP ip,
185 NSVCConfigurationFR fr
186 };
187 type record NSVCConfiguration {
188 NSVCConfigurationP provider,
189 Nsvci nsvci
190 };
191 type record of NSVCConfiguration NSVCConfigurations;
192 type record NSConfiguration {
193 Nsvci nsei,
194 boolean role_sgsn,
195 boolean handle_sns,
196 NSVCConfigurations nsvc
197 }
198
199 /***********************************************************************
200 * per NS-VCG component. Exists once per [peer of] NSE
201 ***********************************************************************/
202
Harald Welte6fff3642017-07-22 21:36:13 +0200203 type component NS_CT {
Harald Welte90f19742020-11-06 19:34:40 +0100204 /* NS-User SAP towards the user */
205 port NS_SP_PT NS_SP;
206
207 /* port towards the per-NSVC components */
Harald Welte1308dbb2021-01-18 18:19:38 +0100208 port NSint_PT NSVC;
Harald Welte90f19742020-11-06 19:34:40 +0100209
Harald Welte3f820892021-01-19 18:19:23 +0100210 /* control port, used to manipulate at runtime */
211 port NS_CTRL_SP_PT NS_CTRL;
212
Harald Welte90f19742020-11-06 19:34:40 +0100213 /* all of the NS configuration a user passes to us */
214 var NSConfiguration g_config;
215 var charstring g_id;
216
217 /* references to the per-NSVC components */
218 var NsvcTable g_nsvcs := {};
Harald Weltec4505522020-11-11 18:55:09 +0100219 /* list of indexes to g_nsvcs[] of currently unblocked NSVCs */
Harald Welteaaa0dfd2021-02-04 16:58:53 +0100220 var Osmocom_Types.ro_integer g_unblocked_nsvcs_sig := {};
221 var Osmocom_Types.ro_integer g_unblocked_nsvcs_data := {};
Harald Welte90f19742020-11-06 19:34:40 +0100222 };
223 type record NsvcTableEntry {
Harald Weltebe7afce2021-01-17 22:04:36 +0100224 NSVCConfiguration cfg,
Harald Welte90f19742020-11-06 19:34:40 +0100225 NSVC_CT vc_conn,
226 NsvcState state
227 };
228 type record of NsvcTableEntry NsvcTable;
229
Harald Welte1308dbb2021-01-18 18:19:38 +0100230 /* internal port from the NS-VC point of view */
231 type port NSint_SP_PT message {
232 in NsUnitdataRequest,
233 SnsRequest,
234 NsCtrlRequest;
235 out NsUnitdataIndication,
236 SnsIndication,
237 NsStatusIndication;
238 } with { extension "internal" };
239
240 /* internal port from the NS-VCG point of view */
241 type port NSint_PT message {
242 in ASP_Event,
243 NsStatusIndication,
244 SnsIndication,
245 NsUnitdataIndication;
246 out NsUnitdataRequest,
247 SnsRequest,
248 NsCtrlRequest;
249 } with { extension "internal" };
250
251 /* Used by NS-VC to report reception of a SNS PDU to NS-VCG */
252 type record SnsIndication {
253 Nsvci nsvci,
254 PDU_NS ns
255 };
256
257 /* Used by NS-VCG to request transmission of a SNS PDU via a NS-VC */
258 type record SnsRequest {
259 Nsvci nsvci,
260 PDU_NS ns
261 };
262
263 type enumerated NsCtrlRequest {
Harald Welte3f820892021-01-19 18:19:23 +0100264 StartAliveProcedure (0),
265 DisableReq (1), /* administratively disable NS-VC */
266 EnableReq (2) /* administratively enable NS-VC */
Harald Welte1308dbb2021-01-18 18:19:38 +0100267 };
268
Harald Welte90f19742020-11-06 19:34:40 +0100269 /* add one NSVC (component and table entry */
270 function f_nsvc_add(NSVCConfiguration nsvc_cfg) runs on NS_CT {
271 var charstring nsvc_id := g_id & "-NSVCI" & int2str(nsvc_cfg.nsvci);
272 var NsvcTableEntry te;
273
Harald Weltebe7afce2021-01-17 22:04:36 +0100274 te.cfg := nsvc_cfg;
Harald Welte90f19742020-11-06 19:34:40 +0100275 te.vc_conn := NSVC_CT.create(nsvc_id);
276 te.state := NSVC_S_DEAD_BLOCKED;
277
278 connect(self:NSVC, te.vc_conn:NS_SP);
279 te.vc_conn.start(NSVCStart(nsvc_cfg, g_config, nsvc_id));
280
281 g_nsvcs := g_nsvcs & { te };
Harald Weltec4505522020-11-11 18:55:09 +0100282 /* no need to add to g_unblocked_nsvcs, as state is always DEAD_BLOCKED above */
Harald Welte90f19742020-11-06 19:34:40 +0100283 }
284
285 function f_nsvc_find_idx(Nsvci nsvci) runs on NS_CT return integer {
286 var integer i;
287 for (i := 0; i < lengthof(g_nsvcs); i := i+1) {
Harald Weltebe7afce2021-01-17 22:04:36 +0100288 if (g_nsvcs[i].cfg.nsvci == nsvci) {
Harald Welte90f19742020-11-06 19:34:40 +0100289 return i;
290 }
291 }
292 return -1;
293 }
294
295 function f_nsvc_find(Nsvci nsvci) runs on NS_CT return NSVC_CT {
296 var integer i := f_nsvc_find_idx(nsvci);
297 if (i < 0) {
298 return null;
299 } else {
300 return g_nsvcs[i].vc_conn;
301 }
302 }
303
304 function f_nsvc_update_state(Nsvci nsvci, NsvcState state) runs on NS_CT {
305 var integer i := f_nsvc_find_idx(nsvci);
306 if (i < 0) {
307 return;
308 }
Harald Weltec4505522020-11-11 18:55:09 +0100309 if (g_nsvcs[i].state != NSVC_S_ALIVE_UNBLOCKED and state == NSVC_S_ALIVE_UNBLOCKED) {
310 /* add index to list of unblocked NSVCs */
Harald Weltebe7afce2021-01-17 22:04:36 +0100311 if (not ischosen(g_nsvcs[i].cfg.provider.ip) or
312 g_nsvcs[i].cfg.provider.ip.signalling_weight > 0) {
Harald Weltedaf89682021-02-04 17:06:20 +0100313 ro_integer_add_unique(g_unblocked_nsvcs_sig, i);
Harald Weltebe7afce2021-01-17 22:04:36 +0100314 }
315 if (not ischosen(g_nsvcs[i].cfg.provider.ip) or
316 g_nsvcs[i].cfg.provider.ip.data_weight > 0) {
Harald Weltedaf89682021-02-04 17:06:20 +0100317 ro_integer_add_unique(g_unblocked_nsvcs_data, i);
Harald Weltebe7afce2021-01-17 22:04:36 +0100318 }
Harald Weltec4505522020-11-11 18:55:09 +0100319 } else if (g_nsvcs[i].state == NSVC_S_ALIVE_UNBLOCKED and state != NSVC_S_ALIVE_UNBLOCKED) {
320 /* remove index to list of unblocked NSVCs */
Harald Weltedaf89682021-02-04 17:06:20 +0100321 ro_integer_del(g_unblocked_nsvcs_sig, i);
322 ro_integer_del(g_unblocked_nsvcs_data, i);
Harald Weltec4505522020-11-11 18:55:09 +0100323 }
Harald Welte90f19742020-11-06 19:34:40 +0100324 g_nsvcs[i].state := state;
325 }
326
327 function NSStart(NSConfiguration init_config, charstring id := testcasename()) runs on NS_CT {
328 g_config := init_config;
329 g_id := id;
330
331 /* iterate over list of NS-VCs and start per-NSVC components */
332 for (var integer i := 0; i < lengthof(g_config.nsvc); i := i+1) {
333 var NSVCConfiguration nsvc_cfg := g_config.nsvc[i];
334 f_nsvc_add(nsvc_cfg);
335 }
336
337 while (true) {
338 alt {
339 [] as_ns_common() {}
340 }
341 }
342 }
343
Harald Weltedc0a0902020-11-10 21:03:29 +0100344 function f_count_nsvcs_in_state(template NsvcState state := ?) runs on NS_CT return integer {
345 var integer i;
346 var integer res := 0;
347 for (i := 0; i < lengthof(g_nsvcs); i := i+1) {
348 if (match(g_nsvcs[i].state, state)) {
349 res := res + 1;
350 }
351 }
352 return res;
353 }
354
Harald Welte516c5c72021-02-13 16:43:58 +0100355 private altstep as_ns_common_status() runs on NS_CT {
Harald Welte90f19742020-11-06 19:34:40 +0100356 var NsStatusIndication rx_nssi;
Harald Weltedc0a0902020-11-10 21:03:29 +0100357 [] NSVC.receive(tr_NsStsInd(g_config.nsei, ?, ?, NSVC_S_ALIVE_UNBLOCKED)) -> value rx_nssi {
358 /* check if this one is the first to be unblocked */
359 var integer num_nsvc_unblocked := f_count_nsvcs_in_state(NSVC_S_ALIVE_UNBLOCKED);
360 f_nsvc_update_state(rx_nssi.nsvci, rx_nssi.new_state);
361 if (num_nsvc_unblocked == 0) {
362 rx_nssi.first_or_last := true;
363 }
364 NS_SP.send(rx_nssi);
365 }
366 [] NSVC.receive(tr_NsStsInd(g_config.nsei, ?, ?, NSVC_S_DEAD_BLOCKED)) -> value rx_nssi {
367 f_nsvc_update_state(rx_nssi.nsvci, rx_nssi.new_state);
368 /* check if this one is the last to be blocked */
369 var integer num_nsvc_unblocked := f_count_nsvcs_in_state(NSVC_S_ALIVE_UNBLOCKED);
370 if (num_nsvc_unblocked == 0) {
371 rx_nssi.first_or_last := true;
372 }
373 NS_SP.send(rx_nssi);
374 }
375 [] NSVC.receive(tr_NsStsInd(g_config.nsei, ?, ?, ?)) -> value rx_nssi {
Harald Welte90f19742020-11-06 19:34:40 +0100376 f_nsvc_update_state(rx_nssi.nsvci, rx_nssi.new_state);
377 NS_SP.send(rx_nssi);
378 }
379 [] NSVC.receive(tr_NsStsInd) -> value rx_nssi {
380 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
381 log2str("Received NsStatusInd for invalid NSEI: ", rx_nssi));
382 }
Harald Welte516c5c72021-02-13 16:43:58 +0100383 }
384
385 private altstep as_ns_common() runs on NS_CT {
386 var NsUnitdataIndication rx_nsudi;
387 var NsUnitdataRequest rx_nsudr;
388 var NsDisableVcRequest rx_disar;
389 var NsEnableVcRequest rx_enar;
390 /* pass from NS-VCs up to user */
391 [] as_ns_common_status();
Harald Welte90f19742020-11-06 19:34:40 +0100392 [] NSVC.receive(tr_NsUdInd(g_config.nsei, ?, ?)) -> value rx_nsudi {
393 NS_SP.send(rx_nsudi);
394 }
Harald Welte1308dbb2021-01-18 18:19:38 +0100395
396 [g_config.handle_sns and g_config.role_sgsn] as_vcg_sns_sgsn();
397
Harald Welte90f19742020-11-06 19:34:40 +0100398 [] NSVC.receive(tr_NsUdInd(?, ?, ?)) -> value rx_nsudi {
399 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
400 log2str("Received UnitDataInd for invalid NSEI: ", rx_nsudi));
401 }
402 /* from user down to NS-VC */
Harald Welte3f820892021-01-19 18:19:23 +0100403 [] NS_CTRL.receive(NsDisableVcRequest:?) -> value rx_disar {
404 var integer nsvc_idx := f_nsvc_find_idx(rx_disar.nsvci);
405 NSVC.send(NsCtrlRequest:DisableReq) to g_nsvcs[nsvc_idx].vc_conn;
406 }
407 [] NS_CTRL.receive(NsEnableVcRequest:?) -> value rx_enar {
408 var integer nsvc_idx := f_nsvc_find_idx(rx_enar.nsvci);
409 NSVC.send(NsCtrlRequest:EnableReq) to g_nsvcs[nsvc_idx].vc_conn;
410 }
Harald Weltebe7afce2021-01-17 22:04:36 +0100411 [] NS_SP.receive(tr_NsUdReq(g_config.nsei, 0, ?, ?, *)) -> value rx_nsudr {
Harald Weltec4505522020-11-11 18:55:09 +0100412 /* load distribution function */
Harald Weltebe7afce2021-01-17 22:04:36 +0100413 var integer nsvc_idx := g_unblocked_nsvcs_sig[rx_nsudr.lsp mod lengthof(g_unblocked_nsvcs_sig)];
Harald Weltec4505522020-11-11 18:55:09 +0100414 NSVC.send(rx_nsudr) to g_nsvcs[nsvc_idx].vc_conn;
Harald Welte90f19742020-11-06 19:34:40 +0100415 }
Harald Weltebe7afce2021-01-17 22:04:36 +0100416 [] NS_SP.receive(tr_NsUdReq(g_config.nsei, ?, ?, ?, *)) -> value rx_nsudr {
417 /* load distribution function */
418 var integer nsvc_idx := g_unblocked_nsvcs_data[rx_nsudr.lsp mod lengthof(g_unblocked_nsvcs_data)];
419 NSVC.send(rx_nsudr) to g_nsvcs[nsvc_idx].vc_conn;
420 }
421
Harald Weltec4505522020-11-11 18:55:09 +0100422 [] NS_SP.receive(tr_NsUdReq(?, ?, ?, ?, *)) -> value rx_nsudr {
Harald Welte90f19742020-11-06 19:34:40 +0100423 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
424 log2str("Received NsUnitdataReq for invalid NSEI: ", rx_nsudr));
425 }
426 }
427
Harald Welte1308dbb2021-01-18 18:19:38 +0100428 /* generate a list of v4 + v6 endpoints based on the NSVConfigurations. This is not strictly
429 * accurate, as we should create a list of _endpoints_, while we actually create a list of
430 * NSVCs. Those are only identical as long as our peer only implements one endpoint */
431 private function gen_sns_ip_elems(out template (omit) IP4_Elements v4_out,
432 out template (omit) IP6_Elements v6_out) runs on NS_CT {
433 var integer i;
434 var IP4_Elements v4 := {};
435 var IP6_Elements v6 := {};
436
437 for (i := 0; i < lengthof(g_config.nsvc); i := i + 1) {
438 var NSVCConfiguration nsvc_cfg := g_config.nsvc[i];
439 if (not ischosen(nsvc_cfg.provider.ip)) {
440 continue;
441 }
442 if (nsvc_cfg.provider.ip.address_family == AF_INET) {
443 v4 := v4 & { valueof(ts_SNS_IPv4(nsvc_cfg.provider.ip.local_ip,
Harald Weltebe7afce2021-01-17 22:04:36 +0100444 nsvc_cfg.provider.ip.local_udp_port,
445 nsvc_cfg.provider.ip.signalling_weight,
446 nsvc_cfg.provider.ip.data_weight)) };
Harald Welte1308dbb2021-01-18 18:19:38 +0100447 } else if (nsvc_cfg.provider.ip.address_family == AF_INET6) {
448 v6 := v6 & { valueof(ts_SNS_IPv6(nsvc_cfg.provider.ip.local_ip,
Harald Weltebe7afce2021-01-17 22:04:36 +0100449 nsvc_cfg.provider.ip.local_udp_port,
450 nsvc_cfg.provider.ip.signalling_weight,
451 nsvc_cfg.provider.ip.data_weight)) };
Harald Welte1308dbb2021-01-18 18:19:38 +0100452 }
453 }
454
455 /* we must not return empty lists, but 'omit' as otherwise we get wrong SNS IEs */
456 if (lengthof(v4) == 0) {
457 v4_out := omit;
458 } else {
459 v4_out := v4;
460 }
461 if (lengthof(v6) == 0) {
462 v6_out := omit;
463 } else {
464 v6_out := v6;
465 }
466 }
467
Harald Welte3bd182f2021-02-13 16:43:22 +0100468 private function f_broadcast_ns_ctrl(template (value) NsCtrlRequest req) runs on NS_CT {
469 for (var integer i := 0; i < lengthof(g_nsvcs); i := i+1) {
470 NSVC.send(req) to g_nsvcs[i].vc_conn;
471 }
472 }
473
Harald Welte1308dbb2021-01-18 18:19:38 +0100474 /* simple IP Sub-Network Service responder for the SGSN side. This is not a full implementation
475 * of the protocol, merely sufficient to make the PCU/BSS side happy to proceed */
476 private altstep as_vcg_sns_sgsn() runs on NS_CT {
477 var SnsIndication sind;
478 var NSVC_CT vc;
479 [] NSVC.receive(SnsIndication:{?, tr_SNS_SIZE(g_config.nsei)}) -> value sind sender vc {
480 /* blindly acknowledge whatever the PCU sends */
481 NSVC.send(SnsRequest:{sind.nsvci, ts_SNS_SIZE_ACK(g_config.nsei, omit)}) to vc;
482 }
483 /* FIXME: We assume our peer has only one endpoint */
484 [] NSVC.receive(SnsIndication:{?, tr_SNS_CONFIG(g_config.nsei, true,
485 {tr_SNS_IPv4(g_config.nsvc[0].provider.ip.remote_ip,
486 g_config.nsvc[0].provider.ip.remote_udp_port)})})
487 -> value sind sender vc {
488 /* blindly acknowledge whatever the PCU sends */
489 NSVC.send(SnsRequest:{sind.nsvci, ts_SNS_CONFIG_ACK(g_config.nsei, omit)}) to vc;
490 /* send a SNS-CONFIG in response and expect a SNS-CONFIG-ACK */
491 var template (omit) IP4_Elements v4;
492 var template (omit) IP6_Elements v6;
493 gen_sns_ip_elems(v4, v6);
494 NSVC.send(SnsRequest:{sind.nsvci,
495 ts_SNS_CONFIG(g_config.nsei, true, v4, v6)}) to vc;
496 alt {
497 [] NSVC.receive(SnsIndication:{sind.nsvci,
498 tr_SNS_CONFIG_ACK(g_config.nsei, omit)}) from vc {
499 /* success */
500 log("SNS Config succeeded. Sending Alive");
501 /* inform all NS-VC that they are now considered alive */
Harald Welte3bd182f2021-02-13 16:43:22 +0100502 f_broadcast_ns_ctrl(NsCtrlRequest:StartAliveProcedure);
Harald Welte1308dbb2021-01-18 18:19:38 +0100503 }
504 [] NSVC.receive(SnsIndication:{sind.nsvci,
505 tr_SNS_CONFIG_ACK(g_config.nsei, ?)}) from vc {
506 setverdict(fail, "Unexpected SNS-CONFIG-NACK");
507 self.stop;
508 }
509 }
510 }
511 [] NSVC.receive(SnsIndication:{?, tr_SNS_CONFIG(g_config.nsei, false, ?)}) { /* ignore */}
512 [] NSVC.receive(SnsIndication:{?, tr_SNS_CONFIG(g_config.nsei, true, ?)}) {
513 setverdict(fail, "Unexpected SNS-CONFIG content");
514 self.stop;
515 }
516 [] NSVC.receive(SnsIndication:{?, tr_SNS_CONFIG(?, ?, ?)}) {
517 setverdict(fail, "SNS-CONFIG from unexpected NSEI");
518 self.stop;
519 }
520 }
521
Harald Welte90f19742020-11-06 19:34:40 +0100522 /***********************************************************************
523 per-NSVC component. Exists once for each NS-VC in the NS-VCG
524 ***********************************************************************/
525
526 type component NSVC_CT {
Harald Welte6fff3642017-07-22 21:36:13 +0200527 /* UDP port towards the bottom (IUT) */
Harald Welte013d65a2020-09-13 14:41:31 +0200528 port NS_PROVIDER_PT NSCP;
529 var NS_Provider_IPL4_CT vc_NSP_IP;
Harald Welte867243a2020-09-13 18:32:32 +0200530#ifdef NS_EMULATION_FR
531 var NS_Provider_FR_CT vc_NSP_FR;
532#endif
Harald Welte013d65a2020-09-13 14:41:31 +0200533
Harald Welte90f19742020-11-06 19:34:40 +0100534 /* port towards the NS_CT */
Harald Welte1308dbb2021-01-18 18:19:38 +0100535 port NSint_SP_PT NS_SP;
Harald Welte6fff3642017-07-22 21:36:13 +0200536
Harald Welte90f19742020-11-06 19:34:40 +0100537 /* configuration passed by the user */
538 var NSVCConfiguration g_nsvc_config;
539 /* we cannot access the NS_CT.config and hence need to copy those */
540 var NSConfiguration g_config;
Alexander Couzens2c12b242018-07-31 00:30:11 +0200541
Harald Welte90f19742020-11-06 19:34:40 +0100542 var NsvcState vc_state := NSVC_S_DEAD_BLOCKED;
Harald Welte6e594f22017-07-23 16:19:35 +0200543
544 timer Tns_alive := 3.0;
545 timer Tns_test := 10.0;
546 timer Tns_block := 10.0;
Harald Welte3dd83552020-09-14 11:38:01 +0200547 timer Tns_reset := 10.0;
Harald Welte6fff3642017-07-22 21:36:13 +0200548 }
549
Harald Welte90f19742020-11-06 19:34:40 +0100550 function NSVCStart(NSVCConfiguration init_config, NSConfiguration init_g_config, charstring id := testcasename()) runs on NSVC_CT {
551 g_nsvc_config := init_config;
552 g_config := init_g_config;
553 f_init(id & "-NSVCemu" & int2str(g_nsvc_config.nsvci));
554 f_ScanEvents();
Alexander Couzens2c12b242018-07-31 00:30:11 +0200555 }
Harald Welte6fff3642017-07-22 21:36:13 +0200556
Harald Welte90f19742020-11-06 19:34:40 +0100557 private function f_init(charstring id) runs on NSVC_CT {
Harald Welte90f19742020-11-06 19:34:40 +0100558 if (ischosen(g_nsvc_config.provider.ip)) {
559 /* Connect the UDP socket */
560 vc_NSP_IP := NS_Provider_IPL4_CT.create(id & "-provIP");
561 connect(self:NSCP, vc_NSP_IP:NSE);
562 vc_NSP_IP.start(NS_Provider_IPL4.main(g_nsvc_config, g_config, id));
563#ifdef NS_EMULATION_FR
564 } else if (ischosen(g_nsvc_config.provider.fr)) {
565 vc_NSP_FR := NS_Provider_FR_CT.create(id & "-provFR");
566 connect(self:NSCP, vc_NSP_FR:NSE);
567 vc_NSP_FR.start(NS_Provider_FR.main(g_nsvc_config, g_config, id));
568#endif
Harald Weltee15299a2020-11-17 17:30:09 +0100569 } else {
570 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unsupported NS provider");
Harald Welte90f19742020-11-06 19:34:40 +0100571 }
572
573 f_change_state(NSVC_S_DEAD_BLOCKED);
Harald Welte6fff3642017-07-22 21:36:13 +0200574 }
575
Harald Welte90f19742020-11-06 19:34:40 +0100576 private function f_change_state(NsvcState new_state) runs on NSVC_CT {
577 var NsvcState old_state := vc_state;
578 vc_state := new_state;
579 log("NSVC ", g_nsvc_config.nsvci, " State Transition: ", old_state, " -> ", new_state);
580 NS_SP.send(ts_NsStsInd(g_config.nsei, g_nsvc_config.nsvci, old_state, new_state));
581 }
582
583 private function f_sendReset() runs on NSVC_CT {
584 NSCP.send(ts_NS_RESET(NS_CAUSE_OM_INTERVENTION, g_nsvc_config.nsvci, g_config.nsei));
Harald Welte3dd83552020-09-14 11:38:01 +0200585 Tns_reset.start;
Harald Welte90f19742020-11-06 19:34:40 +0100586 vc_state := NSVC_S_WAIT_RESET;
Harald Welte6fff3642017-07-22 21:36:13 +0200587 }
588
Harald Welte90f19742020-11-06 19:34:40 +0100589 private function f_sendAlive() runs on NSVC_CT {
Harald Welte013d65a2020-09-13 14:41:31 +0200590 NSCP.send(t_NS_ALIVE);
Harald Welte6e594f22017-07-23 16:19:35 +0200591 Tns_alive.start;
592 }
593
Harald Welte90f19742020-11-06 19:34:40 +0100594 private function f_sendUnblock() runs on NSVC_CT {
Harald Welte013d65a2020-09-13 14:41:31 +0200595 NSCP.send(t_NS_UNBLOCK);
Harald Welte6e594f22017-07-23 16:19:35 +0200596 Tns_block.start;
597 }
598
Harald Welte90f19742020-11-06 19:34:40 +0100599 private function f_sendBlock(NsCause cause) runs on NSVC_CT {
600 NSCP.send(ts_NS_BLOCK(cause, g_nsvc_config.nsvci));
Harald Welte6e594f22017-07-23 16:19:35 +0200601 Tns_block.start;
602 }
603
Harald Welte90f19742020-11-06 19:34:40 +0100604 private altstep as_allstate() runs on NSVC_CT {
Harald Welte013d65a2020-09-13 14:41:31 +0200605 var PDU_NS rf;
Harald Welte6e594f22017-07-23 16:19:35 +0200606 var ASP_Event evt;
607
Harald Weltec0c67ff2021-02-03 19:26:30 +0100608 [] NSCP.receive(NS_Provider_Evt:{link_status:=NS_PROV_LINK_STATUS_DOWN}) {
609 log("Provider Link went down");
610 Tns_test.stop;
611 Tns_alive.stop;
612 f_change_state(NSVC_S_DEAD_BLOCKED);
613 }
614
Harald Welte3f820892021-01-19 18:19:23 +0100615 [] NS_SP.receive(NsCtrlRequest:DisableReq) {
616 /* To make NS-VCG remove us from list of active NS-VC */
617 f_change_state(NSVC_S_DEAD_BLOCKED);
618 log("Disabling NSVC on user request");
619 f_change_state(NSVC_S_DISABLED);
620 Tns_test.stop;
621 Tns_alive.stop;
622 }
623
Harald Welte6e594f22017-07-23 16:19:35 +0200624 /* transition to DEAD if t_alive times out */
Harald Welte9a7c5122020-09-14 11:35:57 +0200625 [] Tns_alive.timeout {
626 log("Tns-alive expired: changing to DEAD_BLOCKED + starting Tns-test");
Harald Welte90f19742020-11-06 19:34:40 +0100627 f_change_state(NSVC_S_DEAD_BLOCKED);
Harald Welte6e594f22017-07-23 16:19:35 +0200628 Tns_test.start;
Harald Welte6fff3642017-07-22 21:36:13 +0200629 }
Harald Welte6fff3642017-07-22 21:36:13 +0200630
Harald Welte9a7c5122020-09-14 11:35:57 +0200631 [] Tns_test.timeout {
Harald Welte6e594f22017-07-23 16:19:35 +0200632 log("Tns-test expired: sending NS-ALIVE");
633 f_sendAlive();
Harald Welte6fff3642017-07-22 21:36:13 +0200634 }
Harald Welte6fff3642017-07-22 21:36:13 +0200635
Harald Welte6e594f22017-07-23 16:19:35 +0200636 /* Stop t_alive when receiving ALIVE-ACK */
Harald Welte9a7c5122020-09-14 11:35:57 +0200637 [Tns_alive.running] NSCP.receive(t_NS_ALIVE_ACK) {
Daniel Willmann654f85e2020-10-12 18:10:06 +0200638 log("Rx NS-ALIVE-ACK: stopping Tns-alive; starting Tns-test");
Harald Welte6e594f22017-07-23 16:19:35 +0200639 Tns_alive.stop;
640 Tns_test.start;
641 }
Harald Welte6fff3642017-07-22 21:36:13 +0200642
Harald Welte6e594f22017-07-23 16:19:35 +0200643 /* respond to NS-ALIVE with NS-ALIVE-ACK */
Harald Welte013d65a2020-09-13 14:41:31 +0200644 [] NSCP.receive(t_NS_ALIVE) {
645 NSCP.send(t_NS_ALIVE_ACK);
Harald Welte6e594f22017-07-23 16:19:35 +0200646 }
647
648 /* Respond to BLOCK for wrong NSVCI */
Harald Welte013d65a2020-09-13 14:41:31 +0200649 [] NSCP.receive(tr_NS_BLOCK(?, ?)) -> value rf {
Harald Welte6e594f22017-07-23 16:19:35 +0200650 log("Rx NS-BLOCK for unknown NSVCI");
651 /* FIXME */
652 }
653
Harald Welte90f19742020-11-06 19:34:40 +0100654 [not g_config.handle_sns] as_handle_reset();
Harald Welte6e594f22017-07-23 16:19:35 +0200655
Harald Welte1308dbb2021-01-18 18:19:38 +0100656 [g_config.handle_sns and ischosen(g_nsvc_config.provider.ip)] as_nsvc_sns();
Harald Welte5e514fa2018-07-05 00:01:45 +0200657
Harald Welte4998a092021-02-04 14:30:47 +0100658 /* log + ignore NS-STATUS; must not create another NS-STATUS in response */
659 [] NSCP.receive(tr_NS_STATUS(?)) -> value rf {
660 log("Rx NS-STATUS ", rf," in state ", vc_state);
661 }
Harald Welte6e594f22017-07-23 16:19:35 +0200662 /* default case of handling unknown PDUs */
Harald Welte013d65a2020-09-13 14:41:31 +0200663 [] NSCP.receive(PDU_NS: ?) -> value rf {
Harald Welte90f19742020-11-06 19:34:40 +0100664 log("Rx Unexpected NS PDU ", rf," in state ", vc_state);
Harald Welte013d65a2020-09-13 14:41:31 +0200665 NSCP.send(ts_NS_STATUS(NS_CAUSE_PDU_NOT_COMPATIBLE_WITH_PROTOCOL_STATE, rf));
Harald Welte6e594f22017-07-23 16:19:35 +0200666 }
Harald Welte6fff3642017-07-22 21:36:13 +0200667 }
668
Harald Welte3f820892021-01-19 18:19:23 +0100669 private altstep as_disabled() runs on NSVC_CT {
670 [g_config.handle_sns == true] NS_SP.receive(NsCtrlRequest:EnableReq) {
671 f_change_state(NSVC_S_ALIVE_UNBLOCKED);
672 f_sendAlive();
673 Tns_test.start;
674 }
675 [g_config.handle_sns == false] NS_SP.receive(NsCtrlRequest:EnableReq) {
676 f_change_state(NSVC_S_DEAD_BLOCKED);
677 Tns_test.start;
678 }
679 /* drop any received messages while in this state */
680 [] NSCP.receive {
681 log("Dropping inbound NS mesage as NS-VC is disabled");
682 }
683 [] NS_SP.receive {
684 log("Dropping user primitive as NS-VC is disabled");
685 }
686 }
687
Harald Welte90f19742020-11-06 19:34:40 +0100688 private altstep as_handle_reset() runs on NSVC_CT {
Daniel Willmann654f85e2020-10-12 18:10:06 +0200689 var PDU_NS rf;
690
Harald Welte90f19742020-11-06 19:34:40 +0100691 [g_config.role_sgsn] NSCP.receive(NS_Provider_Evt:{link_status:=NS_PROV_LINK_STATUS_UP}) {
Daniel Willmann654f85e2020-10-12 18:10:06 +0200692 log("Provider Link came up: waiting for NS-RESET");
693 }
694
Harald Welte90f19742020-11-06 19:34:40 +0100695 [not g_config.role_sgsn] NSCP.receive(NS_Provider_Evt:{link_status:=NS_PROV_LINK_STATUS_UP}) {
Daniel Willmann654f85e2020-10-12 18:10:06 +0200696 log("Provider Link came up: sending NS-RESET");
697 f_sendReset();
698 }
699
700 /* Respond to RESET with correct NSEI/NSVCI */
Harald Welte90f19742020-11-06 19:34:40 +0100701 [] NSCP.receive(tr_NS_RESET(?, g_nsvc_config.nsvci, g_config.nsei)) -> value rf {
702 f_change_state(NSVC_S_ALIVE_BLOCKED);
703 NSCP.send(ts_NS_RESET_ACK(g_nsvc_config.nsvci, g_config.nsei));
Daniel Willmann654f85e2020-10-12 18:10:06 +0200704 log("Rx NS-RESET: Sending NS-ALIVE");
705 f_sendAlive();
706 Tns_test.start;
707 }
708
709 /* Respond to RESET with wrong NSEI/NSVCI */
710 [] NSCP.receive(tr_NS_RESET(?, ?, ?)) -> value rf {
711 log("Rx NS-RESET for unknown NSEI/NSVCI");
712 /* FIXME */
713 }
714 }
715
Harald Welte1308dbb2021-01-18 18:19:38 +0100716 private altstep as_nsvc_sns() runs on NSVC_CT {
Harald Welte013d65a2020-09-13 14:41:31 +0200717 var PDU_NS rf;
Harald Welte1308dbb2021-01-18 18:19:38 +0100718 var SnsRequest sreq;
Alexander Couzensd5338902020-12-21 18:41:21 +0100719 [] NSCP.receive(NS_Provider_Evt:{link_status:=NS_PROV_LINK_STATUS_UP}) {
720 log("Provider Link came up. Waiting for SNS Size");
Harald Welte1308dbb2021-01-18 18:19:38 +0100721 }
Daniel Willmann654f85e2020-10-12 18:10:06 +0200722
Harald Welte1308dbb2021-01-18 18:19:38 +0100723 /* pass up to NS-VCG */
724 [] NSCP.receive(tr_SNS(g_config.nsei)) -> value rf {
725 NS_SP.send(SnsIndication:{g_nsvc_config.nsvci, rf});
Harald Welte5e514fa2018-07-05 00:01:45 +0200726 }
Harald Welte1308dbb2021-01-18 18:19:38 +0100727 [] NSCP.receive(tr_SNS(?)) -> value rf {
728 setverdict(fail, "SNS from unexpected NSEI: ", rf);
Harald Welte5e514fa2018-07-05 00:01:45 +0200729 self.stop;
730 }
Harald Welte1308dbb2021-01-18 18:19:38 +0100731 [] NS_SP.receive(SnsRequest:{g_nsvc_config.nsvci, ?}) -> value sreq {
732 NSCP.send(sreq.ns);
Harald Welte5e514fa2018-07-05 00:01:45 +0200733 }
Harald Welte1308dbb2021-01-18 18:19:38 +0100734 [] NS_SP.receive(SnsRequest:?) -> value sreq {
735 setverdict(fail, "Unexpected SNS from NSVC: ", sreq);
Harald Welte5e514fa2018-07-05 00:01:45 +0200736 self.stop;
737 }
Harald Welte1308dbb2021-01-18 18:19:38 +0100738 [] NS_SP.receive(NsCtrlRequest:StartAliveProcedure) {
739 f_change_state(NSVC_S_ALIVE_UNBLOCKED);
740 f_sendAlive();
741 Tns_test.start;
Harald Welte5e514fa2018-07-05 00:01:45 +0200742 }
743 }
744
Harald Welte90f19742020-11-06 19:34:40 +0100745 private altstep as_alive_blocked() runs on NSVC_CT {
Harald Welte4a6a6632020-09-14 09:58:53 +0200746 var PDU_NS rf;
747 /* bogus block, just respond with ACK */
Harald Welte90f19742020-11-06 19:34:40 +0100748 [] NSCP.receive(tr_NS_BLOCK(?, g_nsvc_config.nsvci)) -> value rf {
749 NSCP.send(ts_NS_BLOCK_ACK(g_nsvc_config.nsvci));
Harald Welte4a6a6632020-09-14 09:58:53 +0200750 }
751 /* Respond to UNBLOCK with UNBLOCK-ACK + change state */
752 [] NSCP.receive(t_NS_UNBLOCK) -> value rf {
753 NSCP.send(t_NS_UNBLOCK_ACK);
754 Tns_block.stop;
Harald Welte90f19742020-11-06 19:34:40 +0100755 f_change_state(NSVC_S_ALIVE_UNBLOCKED);
Harald Welte4a6a6632020-09-14 09:58:53 +0200756 }
757 [] NSCP.receive(t_NS_UNBLOCK_ACK) -> value rf {
758 Tns_block.stop;
Harald Welte90f19742020-11-06 19:34:40 +0100759 f_change_state(NSVC_S_ALIVE_UNBLOCKED);
Harald Welte4a6a6632020-09-14 09:58:53 +0200760 }
Harald Welteda9d9382020-12-07 15:53:57 +0100761 /* tolerate a late NS-BLOCK-ACK from peer */
762 [] NSCP.receive(tr_NS_BLOCK_ACK(g_nsvc_config.nsvci)) -> value rf {
763 }
Harald Welte4a6a6632020-09-14 09:58:53 +0200764 [] Tns_block.timeout {
765 /* repeat unblock transmission */
766 f_sendUnblock();
767 }
768 }
769
Harald Welte90f19742020-11-06 19:34:40 +0100770 private altstep as_alive_unblocked() runs on NSVC_CT {
Harald Welte6fff3642017-07-22 21:36:13 +0200771 var NsUnitdataRequest ud_req;
Harald Welte013d65a2020-09-13 14:41:31 +0200772 var PDU_NS rf;
Harald Welte4a6a6632020-09-14 09:58:53 +0200773 /* bogus unblock, just respond with ACK */
774 [] NSCP.receive(t_NS_UNBLOCK) -> value rf {
775 NSCP.send(t_NS_UNBLOCK_ACK);
776 }
777 /* Respond to BLOCK with BLOCK-ACK + change state */
Harald Welte90f19742020-11-06 19:34:40 +0100778 [] NSCP.receive(tr_NS_BLOCK(?, g_nsvc_config.nsvci)) -> value rf {
779 NSCP.send(ts_NS_BLOCK_ACK(g_nsvc_config.nsvci));
Harald Welte4a6a6632020-09-14 09:58:53 +0200780 Tns_block.stop;
Harald Welte90f19742020-11-06 19:34:40 +0100781 f_change_state(NSVC_S_ALIVE_BLOCKED);
Harald Welte4a6a6632020-09-14 09:58:53 +0200782 }
Harald Welte90f19742020-11-06 19:34:40 +0100783 [] NSCP.receive(tr_NS_BLOCK_ACK(g_nsvc_config.nsvci)) -> value rf {
Harald Welte4a6a6632020-09-14 09:58:53 +0200784 Tns_block.stop;
785 }
Harald Welteda9d9382020-12-07 15:53:57 +0100786 /* tolerate a late NS-UNBLOCK-ACK from peer */
787 [] NSCP.receive(t_NS_UNBLOCK_ACK) -> value rf {
788 }
Harald Weltebe7afce2021-01-17 22:04:36 +0100789
790 [not ischosen(g_nsvc_config.provider.ip) or
791 g_nsvc_config.provider.ip.data_weight > 0] as_alive_unblocked_data();
792
793 [not ischosen(g_nsvc_config.provider.ip) or
794 g_nsvc_config.provider.ip.signalling_weight > 0] as_alive_unblocked_sig();
795
796 /* catch any violations of above rule */
797 [ischosen(g_nsvc_config.provider.ip)] NSCP.receive(tr_NS_UNITDATA(?, ?, ?)) -> value rf {
798 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
799 log2str("Unexpected Rx NS-UNITDATA on NSVC with data_weight=",
800 g_nsvc_config.provider.ip.data_weight, ", sig_weight=",
801 g_nsvc_config.provider.ip.signalling_weight, ": ", rf));
802 }
803 [ischosen(g_nsvc_config.provider.ip)] NS_SP.receive(tr_NsUdReq(g_config.nsei, ?, ?, *, *)) -> value ud_req {
804 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
805 log2str("Unexpected Rx TX-UNITDATA on NSVC with data_weight=",
806 g_nsvc_config.provider.ip.data_weight, ", sig_weight=",
807 g_nsvc_config.provider.ip.signalling_weight, ": ", ud_req));
808 }
809 }
810
811 /* user data transfer; only permitted for some NS-VC */
812 private altstep as_alive_unblocked_data() runs on NSVC_CT {
813 var NsUnitdataRequest ud_req;
814 var PDU_NS rf;
Harald Welte4a6a6632020-09-14 09:58:53 +0200815 /* NS-UNITDATA PDU from network to NS-UNITDATA.ind to user */
Harald Weltebe7afce2021-01-17 22:04:36 +0100816 [] NSCP.receive(tr_NS_UNITDATA_User(?, ?)) -> value rf {
Harald Welte80a249a2020-11-17 19:57:40 +0100817 NS_SP.send(ts_NsUdInd(g_config.nsei, g_nsvc_config.nsvci,
Harald Welte4a6a6632020-09-14 09:58:53 +0200818 oct2int(rf.pDU_NS_Unitdata.bVCI),
819 rf.pDU_NS_Unitdata.nS_SDU));
820 }
821 /* NS-UNITDATA.req from user to NS-UNITDATA PDU on network */
Harald Weltebe7afce2021-01-17 22:04:36 +0100822 [] NS_SP.receive(tr_NsUdReq(g_config.nsei, t_BssgpBvciUser, ?, ?, omit)) -> value ud_req {
Harald Welte4a6a6632020-09-14 09:58:53 +0200823 /* using raw octetstring PDU */
824 NSCP.send(ts_NS_UNITDATA(t_SduCtrlB, ud_req.bvci, ud_req.sdu));
825 }
Harald Weltebe7afce2021-01-17 22:04:36 +0100826 [] NS_SP.receive(tr_NsUdReq(g_config.nsei, t_BssgpBvciUser, ?, omit, ?)) -> value ud_req {
827 /* using decoded BSSGP PDU that we need to encode first */
828 var octetstring enc := enc_PDU_BSSGP(ud_req.bssgp);
829 NSCP.send(ts_NS_UNITDATA(t_SduCtrlB, ud_req.bvci, enc));
830 }
831 }
832
833 /* signalling (BVCI=0) transfer; only permitted for some NS-VC */
834 private altstep as_alive_unblocked_sig() runs on NSVC_CT {
835 var NsUnitdataRequest ud_req;
836 var PDU_NS rf;
837 /* NS-UNITDATA PDU from network to NS-UNITDATA.ind to user */
838 [] NSCP.receive(tr_NS_UNITDATA(?, 0, ?)) -> value rf {
839 NS_SP.send(ts_NsUdInd(g_config.nsei, g_nsvc_config.nsvci,
840 oct2int(rf.pDU_NS_Unitdata.bVCI),
841 rf.pDU_NS_Unitdata.nS_SDU));
842 }
843 /* NS-UNITDATA.req from user to NS-UNITDATA PDU on network */
844 [] NS_SP.receive(tr_NsUdReq(g_config.nsei, 0, ?, ?, omit)) -> value ud_req {
845 /* using raw octetstring PDU */
846 NSCP.send(ts_NS_UNITDATA(t_SduCtrlB, ud_req.bvci, ud_req.sdu));
847 }
848 [] NS_SP.receive(tr_NsUdReq(g_config.nsei, 0, ?, omit, ?)) -> value ud_req {
Harald Welte4a6a6632020-09-14 09:58:53 +0200849 /* using decoded BSSGP PDU that we need to encode first */
850 var octetstring enc := enc_PDU_BSSGP(ud_req.bssgp);
851 NSCP.send(ts_NS_UNITDATA(t_SduCtrlB, ud_req.bvci, enc));
852 }
853 }
Harald Welte6e594f22017-07-23 16:19:35 +0200854
Harald Welte90f19742020-11-06 19:34:40 +0100855 private altstep as_wait_reset() runs on NSVC_CT {
Harald Welte4a6a6632020-09-14 09:58:53 +0200856 var PDU_NS rf;
Harald Welte3dd83552020-09-14 11:38:01 +0200857 [] Tns_reset.timeout {
858 /* If the sending entity of an NS-RESET PDU receives no NS-RESET-ACK PDU before timer
859 * Tns-reset expires the corresponding NS-VCs shall remain blocked and dead and the
860 * entire reset procedure shall be repeated */
861 f_sendReset();
862 }
Harald Welte90f19742020-11-06 19:34:40 +0100863 [] NSCP.receive(tr_NS_RESET_ACK(g_nsvc_config.nsvci, g_config.nsei)) -> value rf {
Harald Welte3dd83552020-09-14 11:38:01 +0200864 Tns_reset.stop;
Harald Welte90f19742020-11-06 19:34:40 +0100865 f_change_state(NSVC_S_ALIVE_BLOCKED);
Harald Welte4a6a6632020-09-14 09:58:53 +0200866 f_sendAlive();
867 f_sendUnblock();
868 }
869 }
Harald Welte6fff3642017-07-22 21:36:13 +0200870
Harald Welte90f19742020-11-06 19:34:40 +0100871 private function f_ScanEvents() runs on NSVC_CT {
Harald Welte4a6a6632020-09-14 09:58:53 +0200872 var PDU_NS rf;
Harald Welte6fff3642017-07-22 21:36:13 +0200873 while (true) {
Harald Welte6fff3642017-07-22 21:36:13 +0200874 alt {
Harald Welte90f19742020-11-06 19:34:40 +0100875 [vc_state == NSVC_S_WAIT_RESET] as_wait_reset();
876 [vc_state == NSVC_S_ALIVE_BLOCKED] as_alive_blocked();
877 [vc_state == NSVC_S_ALIVE_UNBLOCKED] as_alive_unblocked();
Harald Welte3f820892021-01-19 18:19:23 +0100878 [vc_state == NSVC_S_DISABLED] as_disabled();
879 [vc_state != NSVC_S_DISABLED] as_allstate();
Harald Welte6fff3642017-07-22 21:36:13 +0200880 }
881 }
Harald Welte6fff3642017-07-22 21:36:13 +0200882 }
883}