Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 1 | #ifndef _GB_PROXY_H |
| 2 | #define _GB_PROXY_H |
| 3 | |
| 4 | |
| 5 | #include <osmocom/core/msgb.h> |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 6 | #include <osmocom/core/timer.h> |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 7 | #include <osmocom/core/fsm.h> |
Harald Welte | d2fef95 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 8 | #include <osmocom/core/hashtable.h> |
Neels Hofmeyr | 6179f0c | 2018-02-21 00:39:36 +0100 | [diff] [blame] | 9 | #include <osmocom/gsm/gsm23003.h> |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 10 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 11 | #include <osmocom/gprs/gprs_ns2.h> |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 12 | #include <osmocom/vty/command.h> |
| 13 | |
| 14 | #include <sys/types.h> |
| 15 | #include <regex.h> |
Harald Welte | bec7041 | 2019-03-22 09:44:42 +0100 | [diff] [blame] | 16 | #include <stdbool.h> |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 17 | |
| 18 | #define GBPROXY_INIT_VU_GEN_TX 256 |
| 19 | |
Daniel Willmann | 8f407b1 | 2020-12-02 19:33:50 +0100 | [diff] [blame] | 20 | /* BVCI uses 16 bits */ |
| 21 | #define BVC_LOG_CTX_FLAG (1<<17) |
| 22 | |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 23 | struct rate_ctr_group; |
| 24 | struct gprs_gb_parse_context; |
| 25 | struct tlv_parsed; |
| 26 | |
| 27 | enum gbproxy_global_ctr { |
| 28 | GBPROX_GLOB_CTR_INV_BVCI, |
| 29 | GBPROX_GLOB_CTR_INV_LAI, |
| 30 | GBPROX_GLOB_CTR_INV_RAI, |
| 31 | GBPROX_GLOB_CTR_INV_NSEI, |
| 32 | GBPROX_GLOB_CTR_PROTO_ERR_BSS, |
| 33 | GBPROX_GLOB_CTR_PROTO_ERR_SGSN, |
| 34 | GBPROX_GLOB_CTR_NOT_SUPPORTED_BSS, |
| 35 | GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN, |
| 36 | GBPROX_GLOB_CTR_RESTART_RESET_SGSN, |
| 37 | GBPROX_GLOB_CTR_TX_ERR_SGSN, |
| 38 | GBPROX_GLOB_CTR_OTHER_ERR, |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 39 | }; |
| 40 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 41 | enum gbproxy_bvc_ctr { |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 42 | GBPROX_PEER_CTR_BLOCKED, |
| 43 | GBPROX_PEER_CTR_UNBLOCKED, |
| 44 | GBPROX_PEER_CTR_DROPPED, |
| 45 | GBPROX_PEER_CTR_INV_NSEI, |
| 46 | GBPROX_PEER_CTR_TX_ERR, |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 47 | GBPROX_PEER_CTR_LAST, |
| 48 | }; |
| 49 | |
Harald Welte | 4a8769a | 2019-03-22 08:26:45 +0100 | [diff] [blame] | 50 | /* global gb-proxy configuration */ |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 51 | struct gbproxy_config { |
Harald Welte | 4a8769a | 2019-03-22 08:26:45 +0100 | [diff] [blame] | 52 | /* NS instance of libosmogb */ |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 53 | struct gprs_ns2_inst *nsi; |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 54 | |
Harald Welte | 209dc9f | 2020-12-12 19:02:16 +0100 | [diff] [blame] | 55 | struct { |
| 56 | /* percentage of BVC flow control advertised to each SGSN in the pool */ |
| 57 | uint8_t bvc_fc_ratio; |
| 58 | } pool; |
| 59 | |
Harald Welte | 2b7b1bb | 2020-12-04 23:58:20 +0100 | [diff] [blame] | 60 | /* Linked list of all BSS side Gb peers */ |
Harald Welte | d2fef95 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 61 | DECLARE_HASHTABLE(bss_nses, 8); |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 62 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 63 | /* hash table of all SGSN-side Gb peers */ |
| 64 | DECLARE_HASHTABLE(sgsn_nses, 8); |
| 65 | |
| 66 | /* hash table of all gbproxy_cell */ |
| 67 | DECLARE_HASHTABLE(cells, 8); |
| 68 | |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 69 | /* Counter */ |
| 70 | struct rate_ctr_group *ctrg; |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 71 | }; |
| 72 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 73 | /* One Cell within the BSS: Links BSS-side BVC to SGSN-side BVCs */ |
| 74 | struct gbproxy_cell { |
| 75 | /* linked to gbproxy_config.cells hashtable */ |
| 76 | struct hlist_node list; |
| 77 | |
| 78 | /* point back to the config */ |
| 79 | struct gbproxy_config *cfg; |
| 80 | |
| 81 | /* BVCI of PTP BVCs associated to this cell */ |
| 82 | uint16_t bvci; |
| 83 | |
| 84 | /* Routing Area that this BVC is part of (raw 04.08 encoding) */ |
| 85 | uint8_t ra[6]; |
| 86 | |
| 87 | /* pointer to the BSS-side BVC */ |
| 88 | struct gbproxy_bvc *bss_bvc; |
| 89 | |
| 90 | /* pointers to SGSN-side BVC (one for each pool member) */ |
| 91 | struct gbproxy_bvc *sgsn_bvc[16]; |
| 92 | }; |
| 93 | |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 94 | /* One BVC inside an NSE */ |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 95 | struct gbproxy_bvc { |
| 96 | /* linked to gbproxy_nse.bvcs */ |
Harald Welte | 8b4c794 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 97 | struct hlist_node list; |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 98 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 99 | /* The NSE this BVC belongs to */ |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 100 | struct gbproxy_nse *nse; |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 101 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 102 | /* PTP BVCI of this BVC */ |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 103 | uint16_t bvci; |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 104 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 105 | /* Routing Area that this BVC is part of (raw 04.08 encoding) */ |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 106 | uint8_t ra[6]; |
| 107 | |
| 108 | /* Counter */ |
| 109 | struct rate_ctr_group *ctrg; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 110 | |
| 111 | /* the cell to which this BVC belongs */ |
| 112 | struct gbproxy_cell *cell; |
| 113 | |
| 114 | /* per-BVC FSM instance */ |
| 115 | struct osmo_fsm_inst *fi; |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 116 | }; |
| 117 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 118 | /* one NS Entity that we interact with (BSS/PCU) */ |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 119 | struct gbproxy_nse { |
Harald Welte | 2b7b1bb | 2020-12-04 23:58:20 +0100 | [diff] [blame] | 120 | /* linked to gbproxy_config.bss_nses */ |
Harald Welte | d2fef95 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 121 | struct hlist_node list; |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 122 | |
| 123 | /* point back to the config */ |
| 124 | struct gbproxy_config *cfg; |
| 125 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 126 | /* NSEI of the NSE */ |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 127 | uint16_t nsei; |
| 128 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 129 | /* Are we facing towards a SGSN (true) or BSS (false) */ |
| 130 | bool sgsn_facing; |
| 131 | |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 132 | /* List of all BVCs in this NSE */ |
Harald Welte | 8b4c794 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 133 | DECLARE_HASHTABLE(bvcs, 10); |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 134 | }; |
| 135 | |
Daniel Willmann | 066c4cb | 2020-12-01 16:36:29 +0100 | [diff] [blame] | 136 | /* Convenience logging macros for NSE/BVC */ |
| 137 | #define LOGPNSE_CAT(NSE, SUBSYS, LEVEL, FMT, ARGS...) \ |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 138 | LOGP(SUBSYS, LEVEL, "NSE(%05u/%s) " FMT, (NSE)->nsei, \ |
| 139 | (NSE)->sgsn_facing ? "SGSN" : "BSS", ## ARGS) |
Daniel Willmann | 066c4cb | 2020-12-01 16:36:29 +0100 | [diff] [blame] | 140 | #define LOGPNSE(NSE, LEVEL, FMT, ARGS...) \ |
| 141 | LOGPNSE_CAT(NSE, DGPRS, LEVEL, FMT, ## ARGS) |
| 142 | |
| 143 | #define LOGPBVC_CAT(BVC, SUBSYS, LEVEL, FMT, ARGS...) \ |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 144 | LOGP(SUBSYS, LEVEL, "NSE(%05u/%s)-BVC(%05u/%s) " FMT, (BVC)->nse->nsei, \ |
| 145 | (BVC)->nse->sgsn_facing ? "SGSN" : "BSS", (BVC)->bvci, \ |
| 146 | osmo_fsm_inst_state_name((BVC)->fi), ## ARGS) |
Daniel Willmann | 066c4cb | 2020-12-01 16:36:29 +0100 | [diff] [blame] | 147 | #define LOGPBVC(BVC, LEVEL, FMT, ARGS...) \ |
| 148 | LOGPBVC_CAT(BVC, DGPRS, LEVEL, FMT, ## ARGS) |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 149 | |
Harald Welte | cfc7e8e | 2020-12-07 12:03:10 +0100 | [diff] [blame] | 150 | #define LOGPCELL_CAT(CELL, SUBSYS, LEVEL, FMT, ARGS...) \ |
| 151 | LOGP(SUBSYS, LEVEL, "CELL(%05u) " FMT, (CELL)->bvci, ## ARGS) |
| 152 | #define LOGPCELL(CELL, LEVEL, FMT, ARGS...) \ |
| 153 | LOGPCELL_CAT(CELL, DGPRS, LEVEL, FMT, ## ARGS) |
| 154 | |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 155 | /* gb_proxy_vty .c */ |
| 156 | |
| 157 | int gbproxy_vty_init(void); |
| 158 | int gbproxy_parse_config(const char *config_file, struct gbproxy_config *cfg); |
| 159 | |
Daniel Willmann | 13404b7 | 2018-06-01 07:21:20 +0200 | [diff] [blame] | 160 | /* gb_proxy_ctrl.c */ |
| 161 | int gb_ctrl_cmds_install(void); |
| 162 | |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 163 | |
| 164 | /* gb_proxy.c */ |
| 165 | int gbproxy_init_config(struct gbproxy_config *cfg); |
| 166 | |
| 167 | /* Main input function for Gb proxy */ |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 168 | int gbprox_rcvmsg(void *ctx, struct msgb *msg); |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 169 | |
| 170 | int gbprox_signal(unsigned int subsys, unsigned int signal, |
| 171 | void *handler_data, void *signal_data); |
| 172 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 173 | |
| 174 | int gprs_ns2_prim_cb(struct osmo_prim_hdr *oph, void *ctx); |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 175 | |
| 176 | void gbprox_reset(struct gbproxy_config *cfg); |
| 177 | |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 178 | /* Peer handling */ |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 179 | #define NSE_F_SGSN 0x0001 |
| 180 | #define NSE_F_BSS 0x0002 |
| 181 | |
| 182 | struct gbproxy_bvc *gbproxy_bvc_by_bvci(struct gbproxy_nse *nse, uint16_t bvci); |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 183 | struct gbproxy_bvc *gbproxy_bvc_alloc(struct gbproxy_nse *nse, uint16_t bvci); |
| 184 | void gbproxy_bvc_free(struct gbproxy_bvc *bvc); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 185 | int gbproxy_cleanup_bvcs(struct gbproxy_nse *nse, uint16_t bvci); |
| 186 | |
| 187 | struct gbproxy_cell *gbproxy_cell_alloc(struct gbproxy_config *cfg, uint16_t bvci); |
| 188 | struct gbproxy_cell *gbproxy_cell_by_bvci(struct gbproxy_config *cfg, uint16_t bvci); |
| 189 | void gbproxy_cell_free(struct gbproxy_cell *cell); |
| 190 | bool gbproxy_cell_add_sgsn_bvc(struct gbproxy_cell *cell, struct gbproxy_bvc *bvc); |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 191 | |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 192 | /* NSE handling */ |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 193 | struct gbproxy_nse *gbproxy_nse_alloc(struct gbproxy_config *cfg, uint16_t nsei, bool sgsn_facing); |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 194 | void gbproxy_nse_free(struct gbproxy_nse *nse); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 195 | struct gbproxy_nse *gbproxy_nse_by_nsei(struct gbproxy_config *cfg, uint16_t nsei, uint32_t flags); |
| 196 | struct gbproxy_nse *gbproxy_nse_by_nsei_or_new(struct gbproxy_config *cfg, uint16_t nsei, bool sgsn_facing); |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 197 | |
Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 198 | #endif |