blob: 98b3f294192cb480305c393d259226fd33ba42af [file] [log] [blame]
Alexander Couzens6a161492020-07-12 13:45:50 +02001/*! \file gprs_ns2_internal.h */
2
3#pragma once
4
5#include <stdbool.h>
6#include <stdint.h>
7
8#include <osmocom/gprs/protocol/gsm_08_16.h>
9#include <osmocom/gprs/gprs_ns2.h>
10
Harald Weltef2949742021-01-20 14:54:14 +010011#define LOGNSE(nse, lvl, fmt, args ...) \
12 LOGP(DLNS, lvl, "NSE(%05u) " fmt, (nse)->nsei, ## args)
13
14#define LOGBIND(bind, lvl, fmt, args ...) \
15 LOGP(DLNS, lvl, "BIND(%s) " fmt, (bind)->name, ## args)
16
17
18#define LOGNSVC(nsvc, lvl, fmt, args ...) \
19 do { \
20 if ((nsvc)->nsvci_is_valid) { \
21 LOGP(DLNS, lvl, "NSE(%05u)-NSVC(%05u) " fmt, \
22 (nsvc)->nse->nsei, (nsvc)->nsvci, ## args); \
23 } else { \
24 LOGP(DLNS, lvl, "NSE(%05u)-NSVC(none) " fmt, \
25 (nsvc)->nse->nsei, ## args); \
26 } \
27 } while (0)
28
Alexander Couzens6a161492020-07-12 13:45:50 +020029struct osmo_fsm_inst;
30struct tlv_parsed;
31struct vty;
32
33struct gprs_ns2_vc_driver;
34struct gprs_ns2_vc_bind;
35
Alexander Couzens90ee9632020-12-07 06:18:32 +010036#define NS_TIMERS_COUNT 10
Harald Welte3a44d172020-12-16 12:02:51 +010037#define NS_TIMERS "(tns-block|tns-block-retries|tns-reset|tns-reset-retries|tns-test|tns-alive|tns-alive-retries|tsns-prov|tsns-size-retries|tsns-config-retries)"
Alexander Couzens6a161492020-07-12 13:45:50 +020038#define NS_TIMERS_HELP \
39 "(un)blocking Timer (Tns-block) timeout\n" \
40 "(un)blocking Timer (Tns-block) number of retries\n" \
41 "Reset Timer (Tns-reset) timeout\n" \
42 "Reset Timer (Tns-reset) number of retries\n" \
43 "Test Timer (Tns-test) timeout\n" \
44 "Alive Timer (Tns-alive) timeout\n" \
45 "Alive Timer (Tns-alive) number of retries\n" \
Alexander Couzens90ee9632020-12-07 06:18:32 +010046 "SNS Provision Timer (Tsns-prov) timeout\n" \
47 "SNS Size number of retries\n" \
48 "SNS Config number of retries\n" \
Alexander Couzens6a161492020-07-12 13:45:50 +020049
50/* Educated guess - LLC user payload is 1500 bytes plus possible headers */
51#define NS_ALLOC_SIZE 3072
52#define NS_ALLOC_HEADROOM 20
53
54enum ns2_timeout {
55 NS_TOUT_TNS_BLOCK,
56 NS_TOUT_TNS_BLOCK_RETRIES,
57 NS_TOUT_TNS_RESET,
58 NS_TOUT_TNS_RESET_RETRIES,
59 NS_TOUT_TNS_TEST,
60 NS_TOUT_TNS_ALIVE,
61 NS_TOUT_TNS_ALIVE_RETRIES,
62 NS_TOUT_TSNS_PROV,
Alexander Couzens90ee9632020-12-07 06:18:32 +010063 NS_TOUT_TSNS_SIZE_RETRIES,
64 NS_TOUT_TSNS_CONFIG_RETRIES,
Alexander Couzens6a161492020-07-12 13:45:50 +020065};
66
67enum nsvc_timer_mode {
68 /* standard timers */
69 NSVC_TIMER_TNS_TEST,
70 NSVC_TIMER_TNS_ALIVE,
71 NSVC_TIMER_TNS_RESET,
72 _NSVC_TIMER_NR,
73};
74
75enum ns_stat {
76 NS_STAT_ALIVE_DELAY,
77};
78
Alexander Couzens6a161492020-07-12 13:45:50 +020079/*! Osmocom NS2 VC create status */
Alexander Couzensba5a9922021-01-25 16:03:23 +010080enum ns2_cs {
81 NS2_CS_CREATED, /*!< A NSVC object has been created */
82 NS2_CS_FOUND, /*!< A NSVC object has been found */
83 NS2_CS_REJECTED, /*!< Rejected and answered message */
84 NS2_CS_SKIPPED, /*!< Skipped message */
85 NS2_CS_ERROR, /*!< Failed to process message */
Alexander Couzens6a161492020-07-12 13:45:50 +020086};
87
Harald Welte3d5eaee2021-01-30 21:33:02 +010088enum ns_ctr {
89 NS_CTR_PKTS_IN,
90 NS_CTR_PKTS_OUT,
Harald Weltef22ae5a2021-01-30 22:01:28 +010091 NS_CTR_PKTS_OUT_DROP,
Harald Welte3d5eaee2021-01-30 21:33:02 +010092 NS_CTR_BYTES_IN,
93 NS_CTR_BYTES_OUT,
Harald Weltef22ae5a2021-01-30 22:01:28 +010094 NS_CTR_BYTES_OUT_DROP,
Harald Welte3d5eaee2021-01-30 21:33:02 +010095 NS_CTR_BLOCKED,
96 NS_CTR_DEAD,
97 NS_CTR_REPLACED,
98 NS_CTR_NSEI_CHG,
99 NS_CTR_INV_VCI,
100 NS_CTR_INV_NSEI,
101 NS_CTR_LOST_ALIVE,
102 NS_CTR_LOST_RESET,
103};
Alexander Couzens6a161492020-07-12 13:45:50 +0200104
105#define NSE_S_BLOCKED 0x0001
106#define NSE_S_ALIVE 0x0002
107#define NSE_S_RESET 0x0004
108
109#define NS_DESC_B(st) ((st) & NSE_S_BLOCKED ? "BLOCKED" : "UNBLOCKED")
110#define NS_DESC_A(st) ((st) & NSE_S_ALIVE ? "ALIVE" : "DEAD")
111#define NS_DESC_R(st) ((st) & NSE_S_RESET ? "RESET" : "UNRESET")
112
113/*! An instance of the NS protocol stack */
114struct gprs_ns2_inst {
115 /*! callback to the user for incoming UNIT DATA IND */
116 osmo_prim_cb cb;
117
118 /*! callback data */
119 void *cb_data;
120
121 /*! linked lists of all NSVC binds (e.g. IPv4 bind, but could be also E1 */
122 struct llist_head binding;
123
124 /*! linked lists of all NSVC in this instance */
125 struct llist_head nse;
126
Alexander Couzens6a161492020-07-12 13:45:50 +0200127 uint16_t timeout[NS_TIMERS_COUNT];
128
129 /*! workaround for rate counter until rate counter accepts char str as index */
130 uint32_t rate_ctr_idx;
Harald Welte53a2fde2020-12-01 22:21:14 +0100131
132 /*! libmnl netlink socket for link state monitoring */
133 struct osmo_mnl *linkmon_mnl;
Alexander Couzens6a161492020-07-12 13:45:50 +0200134};
135
Harald Welte53a2fde2020-12-01 22:21:14 +0100136
Alexander Couzens6a161492020-07-12 13:45:50 +0200137/*! Structure repesenting a NSE. The BSS/PCU will only have a single NSE, while SGSN has one for each BSS/PCU */
138struct gprs_ns2_nse {
139 uint16_t nsei;
140
141 /*! entry back to ns2_inst */
142 struct gprs_ns2_inst *nsi;
143
144 /*! llist entry for gprs_ns2_inst */
145 struct llist_head list;
146
147 /*! llist head to hold all nsvc */
148 struct llist_head nsvc;
149
Alexander Couzens8a2a1a42020-12-26 18:00:17 +0100150 /*! count all active NSVCs */
151 int nsvc_count;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100152
Alexander Couzens6a161492020-07-12 13:45:50 +0200153 /*! true if this NSE was created by VTY or pcu socket) */
154 bool persistent;
155
Alexander Couzensda0a2852020-10-01 23:24:07 +0200156 /*! true if this NSE wasn't yet alive at all.
157 * Will be true after the first status ind with NS_AFF_CAUSE_RECOVERY */
158 bool first;
159
Alexander Couzens6a161492020-07-12 13:45:50 +0200160 /*! true if this NSE has at least one alive VC */
161 bool alive;
162
Alexander Couzensaac90162020-11-19 02:44:04 +0100163 /*! which link-layer are we based on? */
164 enum gprs_ns2_ll ll;
165
Alexander Couzensd923cff2020-12-01 01:03:52 +0100166 /*! which dialect does this NSE speaks? */
167 enum gprs_ns2_dialect dialect;
168
Alexander Couzens6a161492020-07-12 13:45:50 +0200169 struct osmo_fsm_inst *bss_sns_fi;
Harald Welte3221f872021-01-18 14:58:51 +0100170
171 /*! sum of all the data weight of _active_ NS-VCs */
172 uint32_t sum_data_weight;
173
174 /*! sum of all the signalling weight of _active_ NS-VCs */
175 uint32_t sum_sig_weight;
Alexander Couzens6a161492020-07-12 13:45:50 +0200176};
177
178/*! Structure representing a single NS-VC */
179struct gprs_ns2_vc {
180 /*! list of NS-VCs within NSE */
181 struct llist_head list;
182
183 /*! list of NS-VCs within bind, bind is the owner! */
184 struct llist_head blist;
185
186 /*! pointer to NS Instance */
187 struct gprs_ns2_nse *nse;
188
189 /*! pointer to NS VL bind. bind own the memory of this instance */
190 struct gprs_ns2_vc_bind *bind;
191
192 /*! true if this NS was created by VTY or pcu socket) */
193 bool persistent;
194
195 /*! uniquely identifies NS-VC if VC contains nsvci */
196 uint16_t nsvci;
197
198 /*! signalling weight. 0 = don't use for signalling (BVCI == 0)*/
199 uint8_t sig_weight;
200
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100201 /*! signalling packet counter for the load sharing function */
202 uint8_t sig_counter;
203
204 /*! data weight. 0 = don't use for user data (BVCI != 0) */
Alexander Couzens6a161492020-07-12 13:45:50 +0200205 uint8_t data_weight;
206
207 /*! can be used by the bind/driver of the virtual circuit. e.g. ipv4/ipv6/frgre/e1 */
208 void *priv;
209
210 bool nsvci_is_valid;
211 bool sns_only;
212
213 struct rate_ctr_group *ctrg;
214 struct osmo_stat_item_group *statg;
215
Alexander Couzens6a161492020-07-12 13:45:50 +0200216 enum gprs_ns2_vc_mode mode;
217
218 struct osmo_fsm_inst *fi;
219};
220
221/*! Structure repesenting a bind instance. E.g. IPv4 listen port. */
222struct gprs_ns2_vc_bind {
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100223 /*! unique name */
224 const char *name;
Alexander Couzens6a161492020-07-12 13:45:50 +0200225 /*! list entry in nsi */
226 struct llist_head list;
227 /*! list of all VC */
228 struct llist_head nsvc;
229 /*! driver private structure */
230 void *priv;
231 /*! a pointer back to the nsi */
232 struct gprs_ns2_inst *nsi;
233 struct gprs_ns2_vc_driver *driver;
234
Alexander Couzensd923cff2020-12-01 01:03:52 +0100235 bool accept_ipaccess;
236 bool accept_sns;
Alexander Couzens6a161492020-07-12 13:45:50 +0200237
Alexander Couzens1c8785d2020-12-17 06:58:53 +0100238 /*! transfer capability in mbit */
239 int transfer_capability;
240
Alexander Couzensaac90162020-11-19 02:44:04 +0100241 /*! which link-layer are we based on? */
242 enum gprs_ns2_ll ll;
243
Alexander Couzens6a161492020-07-12 13:45:50 +0200244 /*! send a msg over a VC */
245 int (*send_vc)(struct gprs_ns2_vc *nsvc, struct msgb *msg);
246
247 /*! free the vc priv data */
248 void (*free_vc)(struct gprs_ns2_vc *nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200249
Alexander Couzens22f34712020-10-02 02:34:39 +0200250 /*! allow to show information for the vty */
251 void (*dump_vty)(const struct gprs_ns2_vc_bind *bind,
252 struct vty *vty, bool stats);
253};
Alexander Couzens6a161492020-07-12 13:45:50 +0200254
255struct gprs_ns2_vc_driver {
256 const char *name;
257 void *priv;
258 void (*free_bind)(struct gprs_ns2_vc_bind *driver);
259};
260
Alexander Couzensba5a9922021-01-25 16:03:23 +0100261enum ns2_cs ns2_create_vc(struct gprs_ns2_vc_bind *bind,
Alexander Couzens6a161492020-07-12 13:45:50 +0200262 struct msgb *msg,
263 const char *logname,
264 struct msgb **reject,
265 struct gprs_ns2_vc **success);
266
Alexander Couzensffd49d02020-09-24 00:47:17 +0200267int ns2_recv_vc(struct gprs_ns2_vc *nsvc,
Alexander Couzens6a161492020-07-12 13:45:50 +0200268 struct msgb *msg);
269
270struct gprs_ns2_vc *ns2_vc_alloc(struct gprs_ns2_vc_bind *bind,
271 struct gprs_ns2_nse *nse,
Alexander Couzensd923cff2020-12-01 01:03:52 +0100272 bool initiater,
Harald Welte603f4042020-11-29 17:39:19 +0100273 enum gprs_ns2_vc_mode vc_mode,
274 const char *id);
Alexander Couzens6a161492020-07-12 13:45:50 +0200275
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100276struct msgb *ns2_msgb_alloc(void);
Alexander Couzens6a161492020-07-12 13:45:50 +0200277
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100278void ns2_sns_write_vty(struct vty *vty, const struct gprs_ns2_nse *nse);
279void ns2_sns_dump_vty(struct vty *vty, const char *prefix, const struct gprs_ns2_nse *nse, bool stats);
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200280void ns2_prim_status_ind(struct gprs_ns2_nse *nse,
Daniel Willmann15c09a82020-11-03 23:05:43 +0100281 struct gprs_ns2_vc *nsvc,
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200282 uint16_t bvci,
Alexander Couzens6a161492020-07-12 13:45:50 +0200283 enum gprs_ns2_affecting_cause cause);
284void ns2_nse_notify_alive(struct gprs_ns2_vc *nsvc, bool alive);
285
286/* message */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100287int ns2_validate(struct gprs_ns2_vc *nsvc,
288 uint8_t pdu_type,
289 struct msgb *msg,
290 struct tlv_parsed *tp,
291 uint8_t *cause);
Alexander Couzens6a161492020-07-12 13:45:50 +0200292
293/* SNS messages */
294int ns2_tx_sns_ack(struct gprs_ns2_vc *nsvc, uint8_t trans_id, uint8_t *cause,
295 const struct gprs_ns_ie_ip4_elem *ip4_elems,
296 unsigned int num_ip4_elems,
297 const struct gprs_ns_ie_ip6_elem *ip6_elems,
298 unsigned int num_ip6_elems);
299int ns2_tx_sns_config(struct gprs_ns2_vc *nsvc, bool end_flag,
300 const struct gprs_ns_ie_ip4_elem *ip4_elems,
301 unsigned int num_ip4_elems,
302 const struct gprs_ns_ie_ip6_elem *ip6_elems,
303 unsigned int num_ip6_elems);
304int ns2_tx_sns_config_ack(struct gprs_ns2_vc *nsvc, uint8_t *cause);
305int ns2_tx_sns_size(struct gprs_ns2_vc *nsvc, bool reset_flag, uint16_t max_nr_nsvc,
306 int ip4_ep_nr, int ip6_ep_nr);
307int ns2_tx_sns_size_ack(struct gprs_ns2_vc *nsvc, uint8_t *cause);
308
309/* transmit message over a VC */
310int ns2_tx_block(struct gprs_ns2_vc *nsvc, uint8_t cause);
311int ns2_tx_block_ack(struct gprs_ns2_vc *nsvc);
312
313int ns2_tx_reset(struct gprs_ns2_vc *nsvc, uint8_t cause);
314int ns2_tx_reset_ack(struct gprs_ns2_vc *nsvc);
315
316int ns2_tx_unblock(struct gprs_ns2_vc *nsvc);
317int ns2_tx_unblock_ack(struct gprs_ns2_vc *nsvc);
318
319int ns2_tx_alive(struct gprs_ns2_vc *nsvc);
320int ns2_tx_alive_ack(struct gprs_ns2_vc *nsvc);
321
322int ns2_tx_unit_data(struct gprs_ns2_vc *nsvc,
323 uint16_t bvci, uint8_t sducontrol,
324 struct msgb *msg);
325
326int ns2_tx_status(struct gprs_ns2_vc *nsvc, uint8_t cause,
327 uint16_t bvci, struct msgb *orig_msg);
328
329/* driver */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100330struct gprs_ns2_vc *ns2_ip_bind_connect(struct gprs_ns2_vc_bind *bind,
331 struct gprs_ns2_nse *nse,
332 const struct osmo_sockaddr *remote);
Alexander Couzense769f522020-12-07 07:37:07 +0100333int ns2_ip_count_bind(struct gprs_ns2_inst *nsi, struct osmo_sockaddr *remote);
334struct gprs_ns2_vc_bind *ns2_ip_get_bind_by_index(struct gprs_ns2_inst *nsi,
335 struct osmo_sockaddr *remote,
336 int index);
Alexander Couzens6a161492020-07-12 13:45:50 +0200337
338/* sns */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100339int ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200340struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse,
341 const char *id);
Alexander Couzens6a161492020-07-12 13:45:50 +0200342void ns2_sns_free_nsvc(struct gprs_ns2_vc *nsvc);
343
344/* vc */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100345struct osmo_fsm_inst *ns2_vc_fsm_alloc(struct gprs_ns2_vc *nsvc,
Alexander Couzens6a161492020-07-12 13:45:50 +0200346 const char *id, bool initiate);
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100347int ns2_vc_fsm_start(struct gprs_ns2_vc *nsvc);
348int ns2_vc_force_unconfigured(struct gprs_ns2_vc *nsvc);
349int ns2_vc_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp);
350int ns2_vc_is_alive(struct gprs_ns2_vc *nsvc);
351int ns2_vc_is_unblocked(struct gprs_ns2_vc *nsvc);
Alexander Couzens47afc422021-01-17 20:12:46 +0100352int ns2_vc_block(struct gprs_ns2_vc *nsvc);
353int ns2_vc_unblock(struct gprs_ns2_vc *nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200354
Alexander Couzens6a161492020-07-12 13:45:50 +0200355/* nse */
356void ns2_nse_notify_unblocked(struct gprs_ns2_vc *nsvc, bool unblocked);
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100357enum gprs_ns2_vc_mode ns2_dialect_to_vc_mode(enum gprs_ns2_dialect dialect);
Alexander Couzens1c8785d2020-12-17 06:58:53 +0100358int ns2_count_transfer_cap(struct gprs_ns2_nse *nse,
359 uint16_t bvci);