blob: 85869dae7f670ffa19a189f0e0b5a53f4e295947 [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
Harald Weltead320712021-03-29 21:56:48 +0200217 /* references to the endpoint provider components */
218 var IpEndpointTable g_ip_endpoints := {};
219 /* control port for NS-IP provider */
220 port NSPIP_PROC_PT NSPIP_PROC;
221
Harald Welte90f19742020-11-06 19:34:40 +0100222 /* references to the per-NSVC components */
223 var NsvcTable g_nsvcs := {};
Harald Weltec4505522020-11-11 18:55:09 +0100224 /* list of indexes to g_nsvcs[] of currently unblocked NSVCs */
Harald Welteaaa0dfd2021-02-04 16:58:53 +0100225 var Osmocom_Types.ro_integer g_unblocked_nsvcs_sig := {};
226 var Osmocom_Types.ro_integer g_unblocked_nsvcs_data := {};
Harald Welte90f19742020-11-06 19:34:40 +0100227 };
228 type record NsvcTableEntry {
Harald Weltebe7afce2021-01-17 22:04:36 +0100229 NSVCConfiguration cfg,
Harald Welte90f19742020-11-06 19:34:40 +0100230 NSVC_CT vc_conn,
231 NsvcState state
232 };
233 type record of NsvcTableEntry NsvcTable;
Harald Weltead320712021-03-29 21:56:48 +0200234 type record IpEndpointTableEntry {
235 /* configuration */
236 AddressFamily address_family,
237 PortNumber local_udp_port,
238 charstring local_ip,
239 uint8_t data_weight,
240 uint8_t signalling_weight,
241 /* state */
242 NS_Provider_IPL4_CT provider_ct
243 };
244 type record of IpEndpointTableEntry IpEndpointTable;
Harald Welte90f19742020-11-06 19:34:40 +0100245
Harald Welte1308dbb2021-01-18 18:19:38 +0100246 /* internal port from the NS-VC point of view */
247 type port NSint_SP_PT message {
248 in NsUnitdataRequest,
249 SnsRequest,
250 NsCtrlRequest;
251 out NsUnitdataIndication,
252 SnsIndication,
253 NsStatusIndication;
254 } with { extension "internal" };
255
256 /* internal port from the NS-VCG point of view */
257 type port NSint_PT message {
258 in ASP_Event,
259 NsStatusIndication,
260 SnsIndication,
261 NsUnitdataIndication;
262 out NsUnitdataRequest,
263 SnsRequest,
264 NsCtrlRequest;
265 } with { extension "internal" };
266
267 /* Used by NS-VC to report reception of a SNS PDU to NS-VCG */
268 type record SnsIndication {
269 Nsvci nsvci,
270 PDU_NS ns
271 };
272
273 /* Used by NS-VCG to request transmission of a SNS PDU via a NS-VC */
274 type record SnsRequest {
275 Nsvci nsvci,
276 PDU_NS ns
277 };
278
279 type enumerated NsCtrlRequest {
Harald Welte3f820892021-01-19 18:19:23 +0100280 StartAliveProcedure (0),
281 DisableReq (1), /* administratively disable NS-VC */
Harald Weltec5680002021-02-13 16:45:30 +0100282 EnableReq (2), /* administratively enable NS-VC */
283 ForceAliveState (3)
Harald Welte1308dbb2021-01-18 18:19:38 +0100284 };
285
Harald Weltead320712021-03-29 21:56:48 +0200286 function f_ipep_find(AddressFamily af, PortNumber local_udp_port, charstring local_ip,
287 out IpEndpointTableEntry ret)
288 runs on NS_CT return boolean {
289 var integer i;
290 for (i := 0; i < lengthof(g_ip_endpoints); i := i+1) {
291 var IpEndpointTableEntry ipep := g_ip_endpoints[i];
292 if (ipep.address_family == af and ipep.local_udp_port == local_udp_port and
293 ipep.local_ip == local_ip) {
294 ret := ipep;
295 return true;
296 }
297 }
298 return false
299 }
300
301 /* find or create IP endpoint component for [local part of] nsvc_cfg */
302 function f_ipep_find_or_create(NSVCConfiguration nsvc_cfg)
303 runs on NS_CT return NS_Provider_IPL4_CT {
304 var IpEndpointTableEntry ipep;
305 if (f_ipep_find(nsvc_cfg.provider.ip.address_family,
306 nsvc_cfg.provider.ip.local_udp_port,
307 nsvc_cfg.provider.ip.local_ip, ipep)) {
308 return ipep.provider_ct;
309 } else {
310 var charstring nsvc_id := g_id & "-NSVCI" & int2str(nsvc_cfg.nsvci);
311 ipep := {
312 address_family := nsvc_cfg.provider.ip.address_family,
313 local_udp_port := nsvc_cfg.provider.ip.local_udp_port,
314 local_ip := nsvc_cfg.provider.ip.local_ip,
315 data_weight := nsvc_cfg.provider.ip.data_weight,
316 signalling_weight := nsvc_cfg.provider.ip.signalling_weight,
317 provider_ct := -
318 };
319 /* Create ipep and add it to the list */
320 log("Creating NSIP provider for ", ipep.local_ip, ":",
321 ipep.local_udp_port);
322 ipep.provider_ct := NS_Provider_IPL4_CT.create(nsvc_id & "-provIP");
323 connect(self:NSPIP_PROC, ipep.provider_ct:PROC);
324 ipep.provider_ct.start(NS_Provider_IPL4.main(nsvc_cfg, g_config, nsvc_id));
325 g_ip_endpoints := g_ip_endpoints & { ipep };
326 return ipep.provider_ct;
327 }
328 }
329
Harald Welte90f19742020-11-06 19:34:40 +0100330 /* add one NSVC (component and table entry */
331 function f_nsvc_add(NSVCConfiguration nsvc_cfg) runs on NS_CT {
332 var charstring nsvc_id := g_id & "-NSVCI" & int2str(nsvc_cfg.nsvci);
333 var NsvcTableEntry te;
Harald Weltead320712021-03-29 21:56:48 +0200334 var NS_Provider_IPL4_CT vc_ipep := null;
335
336 /* For the IP provider, we have one provider component per local endpoint,
337 * which we must create before adding the NSVcs (for each remote endpoint)
338 * to it */
339 if (ischosen(nsvc_cfg.provider.ip)) {
340 vc_ipep := f_ipep_find_or_create(nsvc_cfg);
341 }
342
343 /* Start the actual NSVC component */
Harald Welte90f19742020-11-06 19:34:40 +0100344
Harald Weltebe7afce2021-01-17 22:04:36 +0100345 te.cfg := nsvc_cfg;
Harald Welte90f19742020-11-06 19:34:40 +0100346 te.vc_conn := NSVC_CT.create(nsvc_id);
347 te.state := NSVC_S_DEAD_BLOCKED;
348
349 connect(self:NSVC, te.vc_conn:NS_SP);
Harald Weltead320712021-03-29 21:56:48 +0200350 log("Starting NSVC component for ", nsvc_cfg);
351 te.vc_conn.start(NSVCStart(nsvc_cfg, g_config, nsvc_id, vc_ipep));
Harald Welte90f19742020-11-06 19:34:40 +0100352
353 g_nsvcs := g_nsvcs & { te };
Harald Weltec4505522020-11-11 18:55:09 +0100354 /* no need to add to g_unblocked_nsvcs, as state is always DEAD_BLOCKED above */
Harald Weltead320712021-03-29 21:56:48 +0200355
356 /* For the IP provider, we must explicitly associate each NSVC with it */
357 if (ischosen(nsvc_cfg.provider.ip)) {
358 /* this causes NS_Provider_IPL4.f_nsvc_add() to be executed */
359 f_nspip_add_nsvc(vc_ipep, nsvc_cfg.provider.ip.remote_ip,
360 nsvc_cfg.provider.ip.remote_udp_port, te.vc_conn);
361 }
Harald Welte90f19742020-11-06 19:34:40 +0100362 }
363
364 function f_nsvc_find_idx(Nsvci nsvci) runs on NS_CT return integer {
365 var integer i;
366 for (i := 0; i < lengthof(g_nsvcs); i := i+1) {
Harald Weltebe7afce2021-01-17 22:04:36 +0100367 if (g_nsvcs[i].cfg.nsvci == nsvci) {
Harald Welte90f19742020-11-06 19:34:40 +0100368 return i;
369 }
370 }
371 return -1;
372 }
373
374 function f_nsvc_find(Nsvci nsvci) runs on NS_CT return NSVC_CT {
375 var integer i := f_nsvc_find_idx(nsvci);
376 if (i < 0) {
377 return null;
378 } else {
379 return g_nsvcs[i].vc_conn;
380 }
381 }
382
383 function f_nsvc_update_state(Nsvci nsvci, NsvcState state) runs on NS_CT {
384 var integer i := f_nsvc_find_idx(nsvci);
385 if (i < 0) {
386 return;
387 }
Harald Weltec4505522020-11-11 18:55:09 +0100388 if (g_nsvcs[i].state != NSVC_S_ALIVE_UNBLOCKED and state == NSVC_S_ALIVE_UNBLOCKED) {
389 /* add index to list of unblocked NSVCs */
Harald Weltebe7afce2021-01-17 22:04:36 +0100390 if (not ischosen(g_nsvcs[i].cfg.provider.ip) or
391 g_nsvcs[i].cfg.provider.ip.signalling_weight > 0) {
Harald Weltedaf89682021-02-04 17:06:20 +0100392 ro_integer_add_unique(g_unblocked_nsvcs_sig, i);
Harald Weltebe7afce2021-01-17 22:04:36 +0100393 }
394 if (not ischosen(g_nsvcs[i].cfg.provider.ip) or
395 g_nsvcs[i].cfg.provider.ip.data_weight > 0) {
Harald Weltedaf89682021-02-04 17:06:20 +0100396 ro_integer_add_unique(g_unblocked_nsvcs_data, i);
Harald Weltebe7afce2021-01-17 22:04:36 +0100397 }
Harald Weltec4505522020-11-11 18:55:09 +0100398 } else if (g_nsvcs[i].state == NSVC_S_ALIVE_UNBLOCKED and state != NSVC_S_ALIVE_UNBLOCKED) {
399 /* remove index to list of unblocked NSVCs */
Harald Weltedaf89682021-02-04 17:06:20 +0100400 ro_integer_del(g_unblocked_nsvcs_sig, i);
401 ro_integer_del(g_unblocked_nsvcs_data, i);
Harald Weltec4505522020-11-11 18:55:09 +0100402 }
Harald Welte90f19742020-11-06 19:34:40 +0100403 g_nsvcs[i].state := state;
404 }
405
406 function NSStart(NSConfiguration init_config, charstring id := testcasename()) runs on NS_CT {
407 g_config := init_config;
408 g_id := id;
409
410 /* iterate over list of NS-VCs and start per-NSVC components */
411 for (var integer i := 0; i < lengthof(g_config.nsvc); i := i+1) {
412 var NSVCConfiguration nsvc_cfg := g_config.nsvc[i];
413 f_nsvc_add(nsvc_cfg);
414 }
415
416 while (true) {
417 alt {
418 [] as_ns_common() {}
419 }
420 }
421 }
422
Harald Weltedc0a0902020-11-10 21:03:29 +0100423 function f_count_nsvcs_in_state(template NsvcState state := ?) runs on NS_CT return integer {
424 var integer i;
425 var integer res := 0;
426 for (i := 0; i < lengthof(g_nsvcs); i := i+1) {
427 if (match(g_nsvcs[i].state, state)) {
428 res := res + 1;
429 }
430 }
431 return res;
432 }
433
Harald Welte516c5c72021-02-13 16:43:58 +0100434 private altstep as_ns_common_status() runs on NS_CT {
Harald Welte90f19742020-11-06 19:34:40 +0100435 var NsStatusIndication rx_nssi;
Harald Weltedc0a0902020-11-10 21:03:29 +0100436 [] NSVC.receive(tr_NsStsInd(g_config.nsei, ?, ?, NSVC_S_ALIVE_UNBLOCKED)) -> value rx_nssi {
437 /* check if this one is the first to be unblocked */
438 var integer num_nsvc_unblocked := f_count_nsvcs_in_state(NSVC_S_ALIVE_UNBLOCKED);
439 f_nsvc_update_state(rx_nssi.nsvci, rx_nssi.new_state);
440 if (num_nsvc_unblocked == 0) {
441 rx_nssi.first_or_last := true;
442 }
443 NS_SP.send(rx_nssi);
444 }
445 [] NSVC.receive(tr_NsStsInd(g_config.nsei, ?, ?, NSVC_S_DEAD_BLOCKED)) -> value rx_nssi {
446 f_nsvc_update_state(rx_nssi.nsvci, rx_nssi.new_state);
447 /* check if this one is the last to be blocked */
448 var integer num_nsvc_unblocked := f_count_nsvcs_in_state(NSVC_S_ALIVE_UNBLOCKED);
449 if (num_nsvc_unblocked == 0) {
450 rx_nssi.first_or_last := true;
451 }
452 NS_SP.send(rx_nssi);
453 }
454 [] NSVC.receive(tr_NsStsInd(g_config.nsei, ?, ?, ?)) -> value rx_nssi {
Harald Welte90f19742020-11-06 19:34:40 +0100455 f_nsvc_update_state(rx_nssi.nsvci, rx_nssi.new_state);
456 NS_SP.send(rx_nssi);
457 }
458 [] NSVC.receive(tr_NsStsInd) -> value rx_nssi {
459 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
460 log2str("Received NsStatusInd for invalid NSEI: ", rx_nssi));
461 }
Harald Welte516c5c72021-02-13 16:43:58 +0100462 }
463
464 private altstep as_ns_common() runs on NS_CT {
465 var NsUnitdataIndication rx_nsudi;
466 var NsUnitdataRequest rx_nsudr;
467 var NsDisableVcRequest rx_disar;
468 var NsEnableVcRequest rx_enar;
469 /* pass from NS-VCs up to user */
470 [] as_ns_common_status();
Harald Welte90f19742020-11-06 19:34:40 +0100471 [] NSVC.receive(tr_NsUdInd(g_config.nsei, ?, ?)) -> value rx_nsudi {
472 NS_SP.send(rx_nsudi);
473 }
Harald Welte1308dbb2021-01-18 18:19:38 +0100474
475 [g_config.handle_sns and g_config.role_sgsn] as_vcg_sns_sgsn();
476
Harald Welte90f19742020-11-06 19:34:40 +0100477 [] NSVC.receive(tr_NsUdInd(?, ?, ?)) -> value rx_nsudi {
478 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
479 log2str("Received UnitDataInd for invalid NSEI: ", rx_nsudi));
480 }
481 /* from user down to NS-VC */
Harald Welte3f820892021-01-19 18:19:23 +0100482 [] NS_CTRL.receive(NsDisableVcRequest:?) -> value rx_disar {
483 var integer nsvc_idx := f_nsvc_find_idx(rx_disar.nsvci);
484 NSVC.send(NsCtrlRequest:DisableReq) to g_nsvcs[nsvc_idx].vc_conn;
485 }
486 [] NS_CTRL.receive(NsEnableVcRequest:?) -> value rx_enar {
487 var integer nsvc_idx := f_nsvc_find_idx(rx_enar.nsvci);
488 NSVC.send(NsCtrlRequest:EnableReq) to g_nsvcs[nsvc_idx].vc_conn;
489 }
Harald Weltebe7afce2021-01-17 22:04:36 +0100490 [] NS_SP.receive(tr_NsUdReq(g_config.nsei, 0, ?, ?, *)) -> value rx_nsudr {
Harald Weltec4505522020-11-11 18:55:09 +0100491 /* load distribution function */
Harald Weltebe7afce2021-01-17 22:04:36 +0100492 var integer nsvc_idx := g_unblocked_nsvcs_sig[rx_nsudr.lsp mod lengthof(g_unblocked_nsvcs_sig)];
Harald Weltec4505522020-11-11 18:55:09 +0100493 NSVC.send(rx_nsudr) to g_nsvcs[nsvc_idx].vc_conn;
Harald Welte90f19742020-11-06 19:34:40 +0100494 }
Harald Weltebe7afce2021-01-17 22:04:36 +0100495 [] NS_SP.receive(tr_NsUdReq(g_config.nsei, ?, ?, ?, *)) -> value rx_nsudr {
496 /* load distribution function */
497 var integer nsvc_idx := g_unblocked_nsvcs_data[rx_nsudr.lsp mod lengthof(g_unblocked_nsvcs_data)];
498 NSVC.send(rx_nsudr) to g_nsvcs[nsvc_idx].vc_conn;
499 }
500
Harald Weltec4505522020-11-11 18:55:09 +0100501 [] NS_SP.receive(tr_NsUdReq(?, ?, ?, ?, *)) -> value rx_nsudr {
Harald Welte90f19742020-11-06 19:34:40 +0100502 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
503 log2str("Received NsUnitdataReq for invalid NSEI: ", rx_nsudr));
504 }
505 }
506
Harald Welte1308dbb2021-01-18 18:19:38 +0100507 /* generate a list of v4 + v6 endpoints based on the NSVConfigurations. This is not strictly
508 * accurate, as we should create a list of _endpoints_, while we actually create a list of
509 * NSVCs. Those are only identical as long as our peer only implements one endpoint */
510 private function gen_sns_ip_elems(out template (omit) IP4_Elements v4_out,
511 out template (omit) IP6_Elements v6_out) runs on NS_CT {
512 var integer i;
513 var IP4_Elements v4 := {};
514 var IP6_Elements v6 := {};
515
516 for (i := 0; i < lengthof(g_config.nsvc); i := i + 1) {
517 var NSVCConfiguration nsvc_cfg := g_config.nsvc[i];
518 if (not ischosen(nsvc_cfg.provider.ip)) {
519 continue;
520 }
521 if (nsvc_cfg.provider.ip.address_family == AF_INET) {
522 v4 := v4 & { valueof(ts_SNS_IPv4(nsvc_cfg.provider.ip.local_ip,
Harald Weltebe7afce2021-01-17 22:04:36 +0100523 nsvc_cfg.provider.ip.local_udp_port,
524 nsvc_cfg.provider.ip.signalling_weight,
525 nsvc_cfg.provider.ip.data_weight)) };
Harald Welte1308dbb2021-01-18 18:19:38 +0100526 } else if (nsvc_cfg.provider.ip.address_family == AF_INET6) {
527 v6 := v6 & { valueof(ts_SNS_IPv6(nsvc_cfg.provider.ip.local_ip,
Harald Weltebe7afce2021-01-17 22:04:36 +0100528 nsvc_cfg.provider.ip.local_udp_port,
529 nsvc_cfg.provider.ip.signalling_weight,
530 nsvc_cfg.provider.ip.data_weight)) };
Harald Welte1308dbb2021-01-18 18:19:38 +0100531 }
532 }
533
534 /* we must not return empty lists, but 'omit' as otherwise we get wrong SNS IEs */
535 if (lengthof(v4) == 0) {
536 v4_out := omit;
537 } else {
538 v4_out := v4;
539 }
540 if (lengthof(v6) == 0) {
541 v6_out := omit;
542 } else {
543 v6_out := v6;
544 }
545 }
546
Harald Welte3bd182f2021-02-13 16:43:22 +0100547 private function f_broadcast_ns_ctrl(template (value) NsCtrlRequest req) runs on NS_CT {
548 for (var integer i := 0; i < lengthof(g_nsvcs); i := i+1) {
549 NSVC.send(req) to g_nsvcs[i].vc_conn;
550 }
551 }
552
Harald Welte1308dbb2021-01-18 18:19:38 +0100553 /* simple IP Sub-Network Service responder for the SGSN side. This is not a full implementation
554 * of the protocol, merely sufficient to make the PCU/BSS side happy to proceed */
555 private altstep as_vcg_sns_sgsn() runs on NS_CT {
556 var SnsIndication sind;
557 var NSVC_CT vc;
558 [] NSVC.receive(SnsIndication:{?, tr_SNS_SIZE(g_config.nsei)}) -> value sind sender vc {
559 /* blindly acknowledge whatever the PCU sends */
560 NSVC.send(SnsRequest:{sind.nsvci, ts_SNS_SIZE_ACK(g_config.nsei, omit)}) to vc;
561 }
562 /* FIXME: We assume our peer has only one endpoint */
563 [] NSVC.receive(SnsIndication:{?, tr_SNS_CONFIG(g_config.nsei, true,
564 {tr_SNS_IPv4(g_config.nsvc[0].provider.ip.remote_ip,
565 g_config.nsvc[0].provider.ip.remote_udp_port)})})
566 -> value sind sender vc {
567 /* blindly acknowledge whatever the PCU sends */
568 NSVC.send(SnsRequest:{sind.nsvci, ts_SNS_CONFIG_ACK(g_config.nsei, omit)}) to vc;
Harald Weltec5680002021-02-13 16:45:30 +0100569 /* switch to "alive" state already before sending the SNS-CONFIG, as otherwise
570 * there would be a race condition between internally performing the state change
571 * of all related NS-VCs and the first incoming NS-PDU after SNS-CONFIG-ACK */
572 f_broadcast_ns_ctrl(NsCtrlRequest:ForceAliveState);
Harald Welte1308dbb2021-01-18 18:19:38 +0100573 /* send a SNS-CONFIG in response and expect a SNS-CONFIG-ACK */
574 var template (omit) IP4_Elements v4;
575 var template (omit) IP6_Elements v6;
576 gen_sns_ip_elems(v4, v6);
577 NSVC.send(SnsRequest:{sind.nsvci,
578 ts_SNS_CONFIG(g_config.nsei, true, v4, v6)}) to vc;
579 alt {
Harald Weltec5680002021-02-13 16:45:30 +0100580 [] as_ns_common_status() {
581 repeat;
582 }
Harald Welte1308dbb2021-01-18 18:19:38 +0100583 [] NSVC.receive(SnsIndication:{sind.nsvci,
584 tr_SNS_CONFIG_ACK(g_config.nsei, omit)}) from vc {
585 /* success */
586 log("SNS Config succeeded. Sending Alive");
587 /* inform all NS-VC that they are now considered alive */
Harald Welte3bd182f2021-02-13 16:43:22 +0100588 f_broadcast_ns_ctrl(NsCtrlRequest:StartAliveProcedure);
Harald Welte1308dbb2021-01-18 18:19:38 +0100589 }
590 [] NSVC.receive(SnsIndication:{sind.nsvci,
591 tr_SNS_CONFIG_ACK(g_config.nsei, ?)}) from vc {
592 setverdict(fail, "Unexpected SNS-CONFIG-NACK");
593 self.stop;
594 }
595 }
596 }
597 [] NSVC.receive(SnsIndication:{?, tr_SNS_CONFIG(g_config.nsei, false, ?)}) { /* ignore */}
598 [] NSVC.receive(SnsIndication:{?, tr_SNS_CONFIG(g_config.nsei, true, ?)}) {
599 setverdict(fail, "Unexpected SNS-CONFIG content");
600 self.stop;
601 }
602 [] NSVC.receive(SnsIndication:{?, tr_SNS_CONFIG(?, ?, ?)}) {
603 setverdict(fail, "SNS-CONFIG from unexpected NSEI");
604 self.stop;
605 }
606 }
607
Harald Welte90f19742020-11-06 19:34:40 +0100608 /***********************************************************************
609 per-NSVC component. Exists once for each NS-VC in the NS-VCG
610 ***********************************************************************/
611
612 type component NSVC_CT {
Harald Welte6fff3642017-07-22 21:36:13 +0200613 /* UDP port towards the bottom (IUT) */
Harald Welte013d65a2020-09-13 14:41:31 +0200614 port NS_PROVIDER_PT NSCP;
615 var NS_Provider_IPL4_CT vc_NSP_IP;
Harald Welte867243a2020-09-13 18:32:32 +0200616#ifdef NS_EMULATION_FR
617 var NS_Provider_FR_CT vc_NSP_FR;
618#endif
Harald Welte013d65a2020-09-13 14:41:31 +0200619
Harald Welte90f19742020-11-06 19:34:40 +0100620 /* port towards the NS_CT */
Harald Welte1308dbb2021-01-18 18:19:38 +0100621 port NSint_SP_PT NS_SP;
Harald Welte6fff3642017-07-22 21:36:13 +0200622
Harald Welte90f19742020-11-06 19:34:40 +0100623 /* configuration passed by the user */
624 var NSVCConfiguration g_nsvc_config;
625 /* we cannot access the NS_CT.config and hence need to copy those */
626 var NSConfiguration g_config;
Alexander Couzens2c12b242018-07-31 00:30:11 +0200627
Harald Welte90f19742020-11-06 19:34:40 +0100628 var NsvcState vc_state := NSVC_S_DEAD_BLOCKED;
Harald Welte6e594f22017-07-23 16:19:35 +0200629
630 timer Tns_alive := 3.0;
631 timer Tns_test := 10.0;
632 timer Tns_block := 10.0;
Harald Welte3dd83552020-09-14 11:38:01 +0200633 timer Tns_reset := 10.0;
Harald Welte6fff3642017-07-22 21:36:13 +0200634 }
635
Harald Weltead320712021-03-29 21:56:48 +0200636 function NSVCStart(NSVCConfiguration init_config, NSConfiguration init_g_config, charstring id :=
637testcasename(), NS_Provider_IPL4_CT nsp_ip := null) runs on NSVC_CT {
Harald Welte90f19742020-11-06 19:34:40 +0100638 g_nsvc_config := init_config;
639 g_config := init_g_config;
Harald Weltead320712021-03-29 21:56:48 +0200640 f_init(id & "-NSVCemu" & int2str(g_nsvc_config.nsvci), nsp_ip);
Harald Welte90f19742020-11-06 19:34:40 +0100641 f_ScanEvents();
Alexander Couzens2c12b242018-07-31 00:30:11 +0200642 }
Harald Welte6fff3642017-07-22 21:36:13 +0200643
Harald Weltead320712021-03-29 21:56:48 +0200644 private function f_init(charstring id, NS_Provider_IPL4_CT nsp_ip) runs on NSVC_CT {
Harald Welte90f19742020-11-06 19:34:40 +0100645 if (ischosen(g_nsvc_config.provider.ip)) {
Harald Weltead320712021-03-29 21:56:48 +0200646 /* the provider already exists; we just associate with it */
647 vc_NSP_IP := nsp_ip
Harald Welte90f19742020-11-06 19:34:40 +0100648#ifdef NS_EMULATION_FR
649 } else if (ischosen(g_nsvc_config.provider.fr)) {
650 vc_NSP_FR := NS_Provider_FR_CT.create(id & "-provFR");
651 connect(self:NSCP, vc_NSP_FR:NSE);
652 vc_NSP_FR.start(NS_Provider_FR.main(g_nsvc_config, g_config, id));
653#endif
Harald Weltee15299a2020-11-17 17:30:09 +0100654 } else {
655 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Unsupported NS provider");
Harald Welte90f19742020-11-06 19:34:40 +0100656 }
657
658 f_change_state(NSVC_S_DEAD_BLOCKED);
Harald Welte6fff3642017-07-22 21:36:13 +0200659 }
660
Harald Welte90f19742020-11-06 19:34:40 +0100661 private function f_change_state(NsvcState new_state) runs on NSVC_CT {
662 var NsvcState old_state := vc_state;
663 vc_state := new_state;
664 log("NSVC ", g_nsvc_config.nsvci, " State Transition: ", old_state, " -> ", new_state);
665 NS_SP.send(ts_NsStsInd(g_config.nsei, g_nsvc_config.nsvci, old_state, new_state));
666 }
667
668 private function f_sendReset() runs on NSVC_CT {
669 NSCP.send(ts_NS_RESET(NS_CAUSE_OM_INTERVENTION, g_nsvc_config.nsvci, g_config.nsei));
Harald Welte3dd83552020-09-14 11:38:01 +0200670 Tns_reset.start;
Harald Welte90f19742020-11-06 19:34:40 +0100671 vc_state := NSVC_S_WAIT_RESET;
Harald Welte6fff3642017-07-22 21:36:13 +0200672 }
673
Harald Welte90f19742020-11-06 19:34:40 +0100674 private function f_sendAlive() runs on NSVC_CT {
Harald Welte013d65a2020-09-13 14:41:31 +0200675 NSCP.send(t_NS_ALIVE);
Harald Welte6e594f22017-07-23 16:19:35 +0200676 Tns_alive.start;
677 }
678
Harald Welte90f19742020-11-06 19:34:40 +0100679 private function f_sendUnblock() runs on NSVC_CT {
Harald Welte013d65a2020-09-13 14:41:31 +0200680 NSCP.send(t_NS_UNBLOCK);
Harald Welte6e594f22017-07-23 16:19:35 +0200681 Tns_block.start;
682 }
683
Harald Welte90f19742020-11-06 19:34:40 +0100684 private function f_sendBlock(NsCause cause) runs on NSVC_CT {
685 NSCP.send(ts_NS_BLOCK(cause, g_nsvc_config.nsvci));
Harald Welte6e594f22017-07-23 16:19:35 +0200686 Tns_block.start;
687 }
688
Harald Welte90f19742020-11-06 19:34:40 +0100689 private altstep as_allstate() runs on NSVC_CT {
Harald Welte013d65a2020-09-13 14:41:31 +0200690 var PDU_NS rf;
Harald Welte6e594f22017-07-23 16:19:35 +0200691 var ASP_Event evt;
692
Harald Weltec0c67ff2021-02-03 19:26:30 +0100693 [] NSCP.receive(NS_Provider_Evt:{link_status:=NS_PROV_LINK_STATUS_DOWN}) {
694 log("Provider Link went down");
695 Tns_test.stop;
696 Tns_alive.stop;
697 f_change_state(NSVC_S_DEAD_BLOCKED);
698 }
699
Harald Welte3f820892021-01-19 18:19:23 +0100700 [] NS_SP.receive(NsCtrlRequest:DisableReq) {
701 /* To make NS-VCG remove us from list of active NS-VC */
702 f_change_state(NSVC_S_DEAD_BLOCKED);
703 log("Disabling NSVC on user request");
704 f_change_state(NSVC_S_DISABLED);
705 Tns_test.stop;
706 Tns_alive.stop;
707 }
708
Harald Welte6e594f22017-07-23 16:19:35 +0200709 /* transition to DEAD if t_alive times out */
Harald Welte9a7c5122020-09-14 11:35:57 +0200710 [] Tns_alive.timeout {
711 log("Tns-alive expired: changing to DEAD_BLOCKED + starting Tns-test");
Harald Welte90f19742020-11-06 19:34:40 +0100712 f_change_state(NSVC_S_DEAD_BLOCKED);
Harald Welte6e594f22017-07-23 16:19:35 +0200713 Tns_test.start;
Harald Welte6fff3642017-07-22 21:36:13 +0200714 }
Harald Welte6fff3642017-07-22 21:36:13 +0200715
Harald Welte9a7c5122020-09-14 11:35:57 +0200716 [] Tns_test.timeout {
Harald Welte6e594f22017-07-23 16:19:35 +0200717 log("Tns-test expired: sending NS-ALIVE");
718 f_sendAlive();
Harald Welte6fff3642017-07-22 21:36:13 +0200719 }
Harald Welte6fff3642017-07-22 21:36:13 +0200720
Harald Welte6e594f22017-07-23 16:19:35 +0200721 /* Stop t_alive when receiving ALIVE-ACK */
Harald Welte9a7c5122020-09-14 11:35:57 +0200722 [Tns_alive.running] NSCP.receive(t_NS_ALIVE_ACK) {
Daniel Willmann654f85e2020-10-12 18:10:06 +0200723 log("Rx NS-ALIVE-ACK: stopping Tns-alive; starting Tns-test");
Harald Welte6e594f22017-07-23 16:19:35 +0200724 Tns_alive.stop;
725 Tns_test.start;
726 }
Harald Welte6fff3642017-07-22 21:36:13 +0200727
Harald Welte6e594f22017-07-23 16:19:35 +0200728 /* respond to NS-ALIVE with NS-ALIVE-ACK */
Harald Welte013d65a2020-09-13 14:41:31 +0200729 [] NSCP.receive(t_NS_ALIVE) {
730 NSCP.send(t_NS_ALIVE_ACK);
Harald Welte6e594f22017-07-23 16:19:35 +0200731 }
732
733 /* Respond to BLOCK for wrong NSVCI */
Harald Welte013d65a2020-09-13 14:41:31 +0200734 [] NSCP.receive(tr_NS_BLOCK(?, ?)) -> value rf {
Harald Welte6e594f22017-07-23 16:19:35 +0200735 log("Rx NS-BLOCK for unknown NSVCI");
736 /* FIXME */
737 }
738
Harald Welte90f19742020-11-06 19:34:40 +0100739 [not g_config.handle_sns] as_handle_reset();
Harald Welte6e594f22017-07-23 16:19:35 +0200740
Harald Welte1308dbb2021-01-18 18:19:38 +0100741 [g_config.handle_sns and ischosen(g_nsvc_config.provider.ip)] as_nsvc_sns();
Harald Welte5e514fa2018-07-05 00:01:45 +0200742
Harald Welte4998a092021-02-04 14:30:47 +0100743 /* log + ignore NS-STATUS; must not create another NS-STATUS in response */
744 [] NSCP.receive(tr_NS_STATUS(?)) -> value rf {
745 log("Rx NS-STATUS ", rf," in state ", vc_state);
746 }
Harald Welte6e594f22017-07-23 16:19:35 +0200747 /* default case of handling unknown PDUs */
Harald Welte013d65a2020-09-13 14:41:31 +0200748 [] NSCP.receive(PDU_NS: ?) -> value rf {
Harald Welte90f19742020-11-06 19:34:40 +0100749 log("Rx Unexpected NS PDU ", rf," in state ", vc_state);
Harald Welte013d65a2020-09-13 14:41:31 +0200750 NSCP.send(ts_NS_STATUS(NS_CAUSE_PDU_NOT_COMPATIBLE_WITH_PROTOCOL_STATE, rf));
Harald Welte6e594f22017-07-23 16:19:35 +0200751 }
Harald Welte6fff3642017-07-22 21:36:13 +0200752 }
753
Harald Welte3f820892021-01-19 18:19:23 +0100754 private altstep as_disabled() runs on NSVC_CT {
755 [g_config.handle_sns == true] NS_SP.receive(NsCtrlRequest:EnableReq) {
756 f_change_state(NSVC_S_ALIVE_UNBLOCKED);
757 f_sendAlive();
758 Tns_test.start;
759 }
760 [g_config.handle_sns == false] NS_SP.receive(NsCtrlRequest:EnableReq) {
761 f_change_state(NSVC_S_DEAD_BLOCKED);
762 Tns_test.start;
763 }
764 /* drop any received messages while in this state */
765 [] NSCP.receive {
766 log("Dropping inbound NS mesage as NS-VC is disabled");
767 }
768 [] NS_SP.receive {
769 log("Dropping user primitive as NS-VC is disabled");
770 }
771 }
772
Harald Welte90f19742020-11-06 19:34:40 +0100773 private altstep as_handle_reset() runs on NSVC_CT {
Daniel Willmann654f85e2020-10-12 18:10:06 +0200774 var PDU_NS rf;
775
Harald Welte90f19742020-11-06 19:34:40 +0100776 [g_config.role_sgsn] NSCP.receive(NS_Provider_Evt:{link_status:=NS_PROV_LINK_STATUS_UP}) {
Daniel Willmann654f85e2020-10-12 18:10:06 +0200777 log("Provider Link came up: waiting for NS-RESET");
778 }
779
Harald Welte90f19742020-11-06 19:34:40 +0100780 [not g_config.role_sgsn] NSCP.receive(NS_Provider_Evt:{link_status:=NS_PROV_LINK_STATUS_UP}) {
Daniel Willmann654f85e2020-10-12 18:10:06 +0200781 log("Provider Link came up: sending NS-RESET");
782 f_sendReset();
783 }
784
785 /* Respond to RESET with correct NSEI/NSVCI */
Harald Welte90f19742020-11-06 19:34:40 +0100786 [] NSCP.receive(tr_NS_RESET(?, g_nsvc_config.nsvci, g_config.nsei)) -> value rf {
787 f_change_state(NSVC_S_ALIVE_BLOCKED);
788 NSCP.send(ts_NS_RESET_ACK(g_nsvc_config.nsvci, g_config.nsei));
Daniel Willmann654f85e2020-10-12 18:10:06 +0200789 log("Rx NS-RESET: Sending NS-ALIVE");
790 f_sendAlive();
791 Tns_test.start;
792 }
793
794 /* Respond to RESET with wrong NSEI/NSVCI */
795 [] NSCP.receive(tr_NS_RESET(?, ?, ?)) -> value rf {
796 log("Rx NS-RESET for unknown NSEI/NSVCI");
797 /* FIXME */
798 }
799 }
800
Harald Welte1308dbb2021-01-18 18:19:38 +0100801 private altstep as_nsvc_sns() runs on NSVC_CT {
Harald Welte013d65a2020-09-13 14:41:31 +0200802 var PDU_NS rf;
Harald Welte1308dbb2021-01-18 18:19:38 +0100803 var SnsRequest sreq;
Alexander Couzensd5338902020-12-21 18:41:21 +0100804 [] NSCP.receive(NS_Provider_Evt:{link_status:=NS_PROV_LINK_STATUS_UP}) {
805 log("Provider Link came up. Waiting for SNS Size");
Harald Welte1308dbb2021-01-18 18:19:38 +0100806 }
Daniel Willmann654f85e2020-10-12 18:10:06 +0200807
Harald Welte1308dbb2021-01-18 18:19:38 +0100808 /* pass up to NS-VCG */
809 [] NSCP.receive(tr_SNS(g_config.nsei)) -> value rf {
810 NS_SP.send(SnsIndication:{g_nsvc_config.nsvci, rf});
Harald Welte5e514fa2018-07-05 00:01:45 +0200811 }
Harald Welte1308dbb2021-01-18 18:19:38 +0100812 [] NSCP.receive(tr_SNS(?)) -> value rf {
813 setverdict(fail, "SNS from unexpected NSEI: ", rf);
Harald Welte5e514fa2018-07-05 00:01:45 +0200814 self.stop;
815 }
Harald Welte1308dbb2021-01-18 18:19:38 +0100816 [] NS_SP.receive(SnsRequest:{g_nsvc_config.nsvci, ?}) -> value sreq {
817 NSCP.send(sreq.ns);
Harald Welte5e514fa2018-07-05 00:01:45 +0200818 }
Harald Welte1308dbb2021-01-18 18:19:38 +0100819 [] NS_SP.receive(SnsRequest:?) -> value sreq {
820 setverdict(fail, "Unexpected SNS from NSVC: ", sreq);
Harald Welte5e514fa2018-07-05 00:01:45 +0200821 self.stop;
822 }
Harald Weltec5680002021-02-13 16:45:30 +0100823 [] NS_SP.receive(NsCtrlRequest:ForceAliveState) {
Harald Welte1308dbb2021-01-18 18:19:38 +0100824 f_change_state(NSVC_S_ALIVE_UNBLOCKED);
Harald Weltec5680002021-02-13 16:45:30 +0100825 }
826 [] NS_SP.receive(NsCtrlRequest:StartAliveProcedure) {
Harald Welte1308dbb2021-01-18 18:19:38 +0100827 f_sendAlive();
828 Tns_test.start;
Harald Welte5e514fa2018-07-05 00:01:45 +0200829 }
830 }
831
Harald Welte90f19742020-11-06 19:34:40 +0100832 private altstep as_alive_blocked() runs on NSVC_CT {
Harald Welte4a6a6632020-09-14 09:58:53 +0200833 var PDU_NS rf;
834 /* bogus block, just respond with ACK */
Harald Welte90f19742020-11-06 19:34:40 +0100835 [] NSCP.receive(tr_NS_BLOCK(?, g_nsvc_config.nsvci)) -> value rf {
836 NSCP.send(ts_NS_BLOCK_ACK(g_nsvc_config.nsvci));
Harald Welte4a6a6632020-09-14 09:58:53 +0200837 }
838 /* Respond to UNBLOCK with UNBLOCK-ACK + change state */
839 [] NSCP.receive(t_NS_UNBLOCK) -> value rf {
840 NSCP.send(t_NS_UNBLOCK_ACK);
841 Tns_block.stop;
Harald Welte90f19742020-11-06 19:34:40 +0100842 f_change_state(NSVC_S_ALIVE_UNBLOCKED);
Harald Welte4a6a6632020-09-14 09:58:53 +0200843 }
844 [] NSCP.receive(t_NS_UNBLOCK_ACK) -> value rf {
845 Tns_block.stop;
Harald Welte90f19742020-11-06 19:34:40 +0100846 f_change_state(NSVC_S_ALIVE_UNBLOCKED);
Harald Welte4a6a6632020-09-14 09:58:53 +0200847 }
Harald Welteda9d9382020-12-07 15:53:57 +0100848 /* tolerate a late NS-BLOCK-ACK from peer */
849 [] NSCP.receive(tr_NS_BLOCK_ACK(g_nsvc_config.nsvci)) -> value rf {
850 }
Harald Welte4a6a6632020-09-14 09:58:53 +0200851 [] Tns_block.timeout {
852 /* repeat unblock transmission */
853 f_sendUnblock();
854 }
855 }
856
Harald Welte90f19742020-11-06 19:34:40 +0100857 private altstep as_alive_unblocked() runs on NSVC_CT {
Harald Welte6fff3642017-07-22 21:36:13 +0200858 var NsUnitdataRequest ud_req;
Harald Welte013d65a2020-09-13 14:41:31 +0200859 var PDU_NS rf;
Harald Welte4a6a6632020-09-14 09:58:53 +0200860 /* bogus unblock, just respond with ACK */
861 [] NSCP.receive(t_NS_UNBLOCK) -> value rf {
862 NSCP.send(t_NS_UNBLOCK_ACK);
863 }
864 /* Respond to BLOCK with BLOCK-ACK + change state */
Harald Welte90f19742020-11-06 19:34:40 +0100865 [] NSCP.receive(tr_NS_BLOCK(?, g_nsvc_config.nsvci)) -> value rf {
866 NSCP.send(ts_NS_BLOCK_ACK(g_nsvc_config.nsvci));
Harald Welte4a6a6632020-09-14 09:58:53 +0200867 Tns_block.stop;
Harald Welte90f19742020-11-06 19:34:40 +0100868 f_change_state(NSVC_S_ALIVE_BLOCKED);
Harald Welte4a6a6632020-09-14 09:58:53 +0200869 }
Harald Welte90f19742020-11-06 19:34:40 +0100870 [] NSCP.receive(tr_NS_BLOCK_ACK(g_nsvc_config.nsvci)) -> value rf {
Harald Welte4a6a6632020-09-14 09:58:53 +0200871 Tns_block.stop;
872 }
Harald Welteda9d9382020-12-07 15:53:57 +0100873 /* tolerate a late NS-UNBLOCK-ACK from peer */
874 [] NSCP.receive(t_NS_UNBLOCK_ACK) -> value rf {
875 }
Harald Weltebe7afce2021-01-17 22:04:36 +0100876
877 [not ischosen(g_nsvc_config.provider.ip) or
878 g_nsvc_config.provider.ip.data_weight > 0] as_alive_unblocked_data();
879
880 [not ischosen(g_nsvc_config.provider.ip) or
881 g_nsvc_config.provider.ip.signalling_weight > 0] as_alive_unblocked_sig();
882
883 /* catch any violations of above rule */
884 [ischosen(g_nsvc_config.provider.ip)] NSCP.receive(tr_NS_UNITDATA(?, ?, ?)) -> value rf {
885 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
886 log2str("Unexpected Rx NS-UNITDATA on NSVC with data_weight=",
887 g_nsvc_config.provider.ip.data_weight, ", sig_weight=",
888 g_nsvc_config.provider.ip.signalling_weight, ": ", rf));
889 }
890 [ischosen(g_nsvc_config.provider.ip)] NS_SP.receive(tr_NsUdReq(g_config.nsei, ?, ?, *, *)) -> value ud_req {
891 Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
892 log2str("Unexpected Rx TX-UNITDATA on NSVC with data_weight=",
893 g_nsvc_config.provider.ip.data_weight, ", sig_weight=",
894 g_nsvc_config.provider.ip.signalling_weight, ": ", ud_req));
895 }
896 }
897
898 /* user data transfer; only permitted for some NS-VC */
899 private altstep as_alive_unblocked_data() runs on NSVC_CT {
900 var NsUnitdataRequest ud_req;
901 var PDU_NS rf;
Harald Welte4a6a6632020-09-14 09:58:53 +0200902 /* NS-UNITDATA PDU from network to NS-UNITDATA.ind to user */
Harald Weltebe7afce2021-01-17 22:04:36 +0100903 [] NSCP.receive(tr_NS_UNITDATA_User(?, ?)) -> value rf {
Harald Welte80a249a2020-11-17 19:57:40 +0100904 NS_SP.send(ts_NsUdInd(g_config.nsei, g_nsvc_config.nsvci,
Harald Welte4a6a6632020-09-14 09:58:53 +0200905 oct2int(rf.pDU_NS_Unitdata.bVCI),
906 rf.pDU_NS_Unitdata.nS_SDU));
907 }
908 /* NS-UNITDATA.req from user to NS-UNITDATA PDU on network */
Harald Weltebe7afce2021-01-17 22:04:36 +0100909 [] NS_SP.receive(tr_NsUdReq(g_config.nsei, t_BssgpBvciUser, ?, ?, omit)) -> value ud_req {
Harald Welte4a6a6632020-09-14 09:58:53 +0200910 /* using raw octetstring PDU */
911 NSCP.send(ts_NS_UNITDATA(t_SduCtrlB, ud_req.bvci, ud_req.sdu));
912 }
Harald Weltebe7afce2021-01-17 22:04:36 +0100913 [] NS_SP.receive(tr_NsUdReq(g_config.nsei, t_BssgpBvciUser, ?, omit, ?)) -> value ud_req {
914 /* using decoded BSSGP PDU that we need to encode first */
915 var octetstring enc := enc_PDU_BSSGP(ud_req.bssgp);
916 NSCP.send(ts_NS_UNITDATA(t_SduCtrlB, ud_req.bvci, enc));
917 }
918 }
919
920 /* signalling (BVCI=0) transfer; only permitted for some NS-VC */
921 private altstep as_alive_unblocked_sig() runs on NSVC_CT {
922 var NsUnitdataRequest ud_req;
923 var PDU_NS rf;
924 /* NS-UNITDATA PDU from network to NS-UNITDATA.ind to user */
925 [] NSCP.receive(tr_NS_UNITDATA(?, 0, ?)) -> value rf {
926 NS_SP.send(ts_NsUdInd(g_config.nsei, g_nsvc_config.nsvci,
927 oct2int(rf.pDU_NS_Unitdata.bVCI),
928 rf.pDU_NS_Unitdata.nS_SDU));
929 }
930 /* NS-UNITDATA.req from user to NS-UNITDATA PDU on network */
931 [] NS_SP.receive(tr_NsUdReq(g_config.nsei, 0, ?, ?, omit)) -> value ud_req {
932 /* using raw octetstring PDU */
933 NSCP.send(ts_NS_UNITDATA(t_SduCtrlB, ud_req.bvci, ud_req.sdu));
934 }
935 [] NS_SP.receive(tr_NsUdReq(g_config.nsei, 0, ?, omit, ?)) -> value ud_req {
Harald Welte4a6a6632020-09-14 09:58:53 +0200936 /* using decoded BSSGP PDU that we need to encode first */
937 var octetstring enc := enc_PDU_BSSGP(ud_req.bssgp);
938 NSCP.send(ts_NS_UNITDATA(t_SduCtrlB, ud_req.bvci, enc));
939 }
940 }
Harald Welte6e594f22017-07-23 16:19:35 +0200941
Harald Welte90f19742020-11-06 19:34:40 +0100942 private altstep as_wait_reset() runs on NSVC_CT {
Harald Welte4a6a6632020-09-14 09:58:53 +0200943 var PDU_NS rf;
Harald Welte3dd83552020-09-14 11:38:01 +0200944 [] Tns_reset.timeout {
945 /* If the sending entity of an NS-RESET PDU receives no NS-RESET-ACK PDU before timer
946 * Tns-reset expires the corresponding NS-VCs shall remain blocked and dead and the
947 * entire reset procedure shall be repeated */
948 f_sendReset();
949 }
Harald Welte90f19742020-11-06 19:34:40 +0100950 [] NSCP.receive(tr_NS_RESET_ACK(g_nsvc_config.nsvci, g_config.nsei)) -> value rf {
Harald Welte3dd83552020-09-14 11:38:01 +0200951 Tns_reset.stop;
Harald Welte90f19742020-11-06 19:34:40 +0100952 f_change_state(NSVC_S_ALIVE_BLOCKED);
Harald Welte4a6a6632020-09-14 09:58:53 +0200953 f_sendAlive();
954 f_sendUnblock();
955 }
956 }
Harald Welte6fff3642017-07-22 21:36:13 +0200957
Harald Welte90f19742020-11-06 19:34:40 +0100958 private function f_ScanEvents() runs on NSVC_CT {
Harald Welte4a6a6632020-09-14 09:58:53 +0200959 var PDU_NS rf;
Harald Welte6fff3642017-07-22 21:36:13 +0200960 while (true) {
Harald Welte6fff3642017-07-22 21:36:13 +0200961 alt {
Harald Welte90f19742020-11-06 19:34:40 +0100962 [vc_state == NSVC_S_WAIT_RESET] as_wait_reset();
963 [vc_state == NSVC_S_ALIVE_BLOCKED] as_alive_blocked();
964 [vc_state == NSVC_S_ALIVE_UNBLOCKED] as_alive_unblocked();
Harald Welte3f820892021-01-19 18:19:23 +0100965 [vc_state == NSVC_S_DISABLED] as_disabled();
966 [vc_state != NSVC_S_DISABLED] as_allstate();
Harald Welte6fff3642017-07-22 21:36:13 +0200967 }
968 }
Harald Welte6fff3642017-07-22 21:36:13 +0200969 }
970}