blob: 027071d9aca82381bbdf3a87b6a91cc3fe50e32e [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
88
89#define NSE_S_BLOCKED 0x0001
90#define NSE_S_ALIVE 0x0002
91#define NSE_S_RESET 0x0004
92
93#define NS_DESC_B(st) ((st) & NSE_S_BLOCKED ? "BLOCKED" : "UNBLOCKED")
94#define NS_DESC_A(st) ((st) & NSE_S_ALIVE ? "ALIVE" : "DEAD")
95#define NS_DESC_R(st) ((st) & NSE_S_RESET ? "RESET" : "UNRESET")
96
97/*! An instance of the NS protocol stack */
98struct gprs_ns2_inst {
99 /*! callback to the user for incoming UNIT DATA IND */
100 osmo_prim_cb cb;
101
102 /*! callback data */
103 void *cb_data;
104
105 /*! linked lists of all NSVC binds (e.g. IPv4 bind, but could be also E1 */
106 struct llist_head binding;
107
108 /*! linked lists of all NSVC in this instance */
109 struct llist_head nse;
110
111 /*! create dynamic NSE on receiving packages */
112 bool create_nse;
113
114 uint16_t timeout[NS_TIMERS_COUNT];
115
116 /*! workaround for rate counter until rate counter accepts char str as index */
117 uint32_t rate_ctr_idx;
Harald Welte53a2fde2020-12-01 22:21:14 +0100118
119 /*! libmnl netlink socket for link state monitoring */
120 struct osmo_mnl *linkmon_mnl;
Alexander Couzens6a161492020-07-12 13:45:50 +0200121};
122
Harald Welte53a2fde2020-12-01 22:21:14 +0100123
Alexander Couzens6a161492020-07-12 13:45:50 +0200124/*! Structure repesenting a NSE. The BSS/PCU will only have a single NSE, while SGSN has one for each BSS/PCU */
125struct gprs_ns2_nse {
126 uint16_t nsei;
127
128 /*! entry back to ns2_inst */
129 struct gprs_ns2_inst *nsi;
130
131 /*! llist entry for gprs_ns2_inst */
132 struct llist_head list;
133
134 /*! llist head to hold all nsvc */
135 struct llist_head nsvc;
136
Alexander Couzens8a2a1a42020-12-26 18:00:17 +0100137 /*! count all active NSVCs */
138 int nsvc_count;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100139
Alexander Couzens6a161492020-07-12 13:45:50 +0200140 /*! true if this NSE was created by VTY or pcu socket) */
141 bool persistent;
142
Alexander Couzensda0a2852020-10-01 23:24:07 +0200143 /*! true if this NSE wasn't yet alive at all.
144 * Will be true after the first status ind with NS_AFF_CAUSE_RECOVERY */
145 bool first;
146
Alexander Couzens6a161492020-07-12 13:45:50 +0200147 /*! true if this NSE has at least one alive VC */
148 bool alive;
149
Alexander Couzensaac90162020-11-19 02:44:04 +0100150 /*! which link-layer are we based on? */
151 enum gprs_ns2_ll ll;
152
Alexander Couzensd923cff2020-12-01 01:03:52 +0100153 /*! which dialect does this NSE speaks? */
154 enum gprs_ns2_dialect dialect;
155
Alexander Couzens6a161492020-07-12 13:45:50 +0200156 struct osmo_fsm_inst *bss_sns_fi;
Harald Welte3221f872021-01-18 14:58:51 +0100157
158 /*! sum of all the data weight of _active_ NS-VCs */
159 uint32_t sum_data_weight;
160
161 /*! sum of all the signalling weight of _active_ NS-VCs */
162 uint32_t sum_sig_weight;
Alexander Couzens6a161492020-07-12 13:45:50 +0200163};
164
165/*! Structure representing a single NS-VC */
166struct gprs_ns2_vc {
167 /*! list of NS-VCs within NSE */
168 struct llist_head list;
169
170 /*! list of NS-VCs within bind, bind is the owner! */
171 struct llist_head blist;
172
173 /*! pointer to NS Instance */
174 struct gprs_ns2_nse *nse;
175
176 /*! pointer to NS VL bind. bind own the memory of this instance */
177 struct gprs_ns2_vc_bind *bind;
178
179 /*! true if this NS was created by VTY or pcu socket) */
180 bool persistent;
181
182 /*! uniquely identifies NS-VC if VC contains nsvci */
183 uint16_t nsvci;
184
185 /*! signalling weight. 0 = don't use for signalling (BVCI == 0)*/
186 uint8_t sig_weight;
187
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100188 /*! signalling packet counter for the load sharing function */
189 uint8_t sig_counter;
190
191 /*! data weight. 0 = don't use for user data (BVCI != 0) */
Alexander Couzens6a161492020-07-12 13:45:50 +0200192 uint8_t data_weight;
193
194 /*! can be used by the bind/driver of the virtual circuit. e.g. ipv4/ipv6/frgre/e1 */
195 void *priv;
196
197 bool nsvci_is_valid;
198 bool sns_only;
199
200 struct rate_ctr_group *ctrg;
201 struct osmo_stat_item_group *statg;
202
Alexander Couzens6a161492020-07-12 13:45:50 +0200203 enum gprs_ns2_vc_mode mode;
204
205 struct osmo_fsm_inst *fi;
206};
207
208/*! Structure repesenting a bind instance. E.g. IPv4 listen port. */
209struct gprs_ns2_vc_bind {
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100210 /*! unique name */
211 const char *name;
Alexander Couzens6a161492020-07-12 13:45:50 +0200212 /*! list entry in nsi */
213 struct llist_head list;
214 /*! list of all VC */
215 struct llist_head nsvc;
216 /*! driver private structure */
217 void *priv;
218 /*! a pointer back to the nsi */
219 struct gprs_ns2_inst *nsi;
220 struct gprs_ns2_vc_driver *driver;
221
Alexander Couzensd923cff2020-12-01 01:03:52 +0100222 bool accept_ipaccess;
223 bool accept_sns;
Alexander Couzens6a161492020-07-12 13:45:50 +0200224
Alexander Couzens1c8785d2020-12-17 06:58:53 +0100225 /*! transfer capability in mbit */
226 int transfer_capability;
227
Alexander Couzensaac90162020-11-19 02:44:04 +0100228 /*! which link-layer are we based on? */
229 enum gprs_ns2_ll ll;
230
Alexander Couzens6a161492020-07-12 13:45:50 +0200231 /*! send a msg over a VC */
232 int (*send_vc)(struct gprs_ns2_vc *nsvc, struct msgb *msg);
233
234 /*! free the vc priv data */
235 void (*free_vc)(struct gprs_ns2_vc *nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200236
Alexander Couzens22f34712020-10-02 02:34:39 +0200237 /*! allow to show information for the vty */
238 void (*dump_vty)(const struct gprs_ns2_vc_bind *bind,
239 struct vty *vty, bool stats);
240};
Alexander Couzens6a161492020-07-12 13:45:50 +0200241
242struct gprs_ns2_vc_driver {
243 const char *name;
244 void *priv;
245 void (*free_bind)(struct gprs_ns2_vc_bind *driver);
246};
247
Alexander Couzensba5a9922021-01-25 16:03:23 +0100248enum ns2_cs ns2_create_vc(struct gprs_ns2_vc_bind *bind,
Alexander Couzens6a161492020-07-12 13:45:50 +0200249 struct msgb *msg,
250 const char *logname,
251 struct msgb **reject,
252 struct gprs_ns2_vc **success);
253
Alexander Couzensffd49d02020-09-24 00:47:17 +0200254int ns2_recv_vc(struct gprs_ns2_vc *nsvc,
Alexander Couzens6a161492020-07-12 13:45:50 +0200255 struct msgb *msg);
256
257struct gprs_ns2_vc *ns2_vc_alloc(struct gprs_ns2_vc_bind *bind,
258 struct gprs_ns2_nse *nse,
Alexander Couzensd923cff2020-12-01 01:03:52 +0100259 bool initiater,
Harald Welte603f4042020-11-29 17:39:19 +0100260 enum gprs_ns2_vc_mode vc_mode,
261 const char *id);
Alexander Couzens6a161492020-07-12 13:45:50 +0200262
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100263struct msgb *ns2_msgb_alloc(void);
Alexander Couzens6a161492020-07-12 13:45:50 +0200264
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100265void ns2_sns_write_vty(struct vty *vty, const struct gprs_ns2_nse *nse);
266void 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 +0200267void ns2_prim_status_ind(struct gprs_ns2_nse *nse,
Daniel Willmann15c09a82020-11-03 23:05:43 +0100268 struct gprs_ns2_vc *nsvc,
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200269 uint16_t bvci,
Alexander Couzens6a161492020-07-12 13:45:50 +0200270 enum gprs_ns2_affecting_cause cause);
271void ns2_nse_notify_alive(struct gprs_ns2_vc *nsvc, bool alive);
272
273/* message */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100274int ns2_validate(struct gprs_ns2_vc *nsvc,
275 uint8_t pdu_type,
276 struct msgb *msg,
277 struct tlv_parsed *tp,
278 uint8_t *cause);
Alexander Couzens6a161492020-07-12 13:45:50 +0200279
280/* SNS messages */
281int ns2_tx_sns_ack(struct gprs_ns2_vc *nsvc, uint8_t trans_id, uint8_t *cause,
282 const struct gprs_ns_ie_ip4_elem *ip4_elems,
283 unsigned int num_ip4_elems,
284 const struct gprs_ns_ie_ip6_elem *ip6_elems,
285 unsigned int num_ip6_elems);
286int ns2_tx_sns_config(struct gprs_ns2_vc *nsvc, bool end_flag,
287 const struct gprs_ns_ie_ip4_elem *ip4_elems,
288 unsigned int num_ip4_elems,
289 const struct gprs_ns_ie_ip6_elem *ip6_elems,
290 unsigned int num_ip6_elems);
291int ns2_tx_sns_config_ack(struct gprs_ns2_vc *nsvc, uint8_t *cause);
292int ns2_tx_sns_size(struct gprs_ns2_vc *nsvc, bool reset_flag, uint16_t max_nr_nsvc,
293 int ip4_ep_nr, int ip6_ep_nr);
294int ns2_tx_sns_size_ack(struct gprs_ns2_vc *nsvc, uint8_t *cause);
295
296/* transmit message over a VC */
297int ns2_tx_block(struct gprs_ns2_vc *nsvc, uint8_t cause);
298int ns2_tx_block_ack(struct gprs_ns2_vc *nsvc);
299
300int ns2_tx_reset(struct gprs_ns2_vc *nsvc, uint8_t cause);
301int ns2_tx_reset_ack(struct gprs_ns2_vc *nsvc);
302
303int ns2_tx_unblock(struct gprs_ns2_vc *nsvc);
304int ns2_tx_unblock_ack(struct gprs_ns2_vc *nsvc);
305
306int ns2_tx_alive(struct gprs_ns2_vc *nsvc);
307int ns2_tx_alive_ack(struct gprs_ns2_vc *nsvc);
308
309int ns2_tx_unit_data(struct gprs_ns2_vc *nsvc,
310 uint16_t bvci, uint8_t sducontrol,
311 struct msgb *msg);
312
313int ns2_tx_status(struct gprs_ns2_vc *nsvc, uint8_t cause,
314 uint16_t bvci, struct msgb *orig_msg);
315
316/* driver */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100317struct gprs_ns2_vc *ns2_ip_bind_connect(struct gprs_ns2_vc_bind *bind,
318 struct gprs_ns2_nse *nse,
319 const struct osmo_sockaddr *remote);
Alexander Couzense769f522020-12-07 07:37:07 +0100320int ns2_ip_count_bind(struct gprs_ns2_inst *nsi, struct osmo_sockaddr *remote);
321struct gprs_ns2_vc_bind *ns2_ip_get_bind_by_index(struct gprs_ns2_inst *nsi,
322 struct osmo_sockaddr *remote,
323 int index);
Alexander Couzens6a161492020-07-12 13:45:50 +0200324
325/* sns */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100326int ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp);
Alexander Couzens6a161492020-07-12 13:45:50 +0200327struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse,
328 const char *id);
Alexander Couzens6a161492020-07-12 13:45:50 +0200329void ns2_sns_free_nsvc(struct gprs_ns2_vc *nsvc);
330
331/* vc */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100332struct osmo_fsm_inst *ns2_vc_fsm_alloc(struct gprs_ns2_vc *nsvc,
Alexander Couzens6a161492020-07-12 13:45:50 +0200333 const char *id, bool initiate);
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100334int ns2_vc_fsm_start(struct gprs_ns2_vc *nsvc);
335int ns2_vc_force_unconfigured(struct gprs_ns2_vc *nsvc);
336int ns2_vc_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp);
337int ns2_vc_is_alive(struct gprs_ns2_vc *nsvc);
338int ns2_vc_is_unblocked(struct gprs_ns2_vc *nsvc);
Alexander Couzens47afc422021-01-17 20:12:46 +0100339int ns2_vc_block(struct gprs_ns2_vc *nsvc);
340int ns2_vc_unblock(struct gprs_ns2_vc *nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200341
Alexander Couzens6a161492020-07-12 13:45:50 +0200342/* nse */
343void ns2_nse_notify_unblocked(struct gprs_ns2_vc *nsvc, bool unblocked);
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100344enum gprs_ns2_vc_mode ns2_dialect_to_vc_mode(enum gprs_ns2_dialect dialect);
Alexander Couzens1c8785d2020-12-17 06:58:53 +0100345int ns2_count_transfer_cap(struct gprs_ns2_nse *nse,
346 uint16_t bvci);