<p>daniel has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/c/osmo-sgsn/+/21881">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">gbproxy: Add SGSN pooling support<br><br>Change-Id: I58b9f55065f6bd43450e4b07cffe7ba132b1fd9b<br>Related: OS#4472<br>---<br>M include/osmocom/sgsn/gb_proxy.h<br>M src/gbproxy/gb_proxy.c<br>M src/gbproxy/gb_proxy_main.c<br>M src/gbproxy/gb_proxy_peer.c<br>4 files changed, 202 insertions(+), 36 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/osmo-sgsn refs/changes/81/21881/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/include/osmocom/sgsn/gb_proxy.h b/include/osmocom/sgsn/gb_proxy.h</span><br><span>index 46decc0..dd529a3 100644</span><br><span>--- a/include/osmocom/sgsn/gb_proxy.h</span><br><span>+++ b/include/osmocom/sgsn/gb_proxy.h</span><br><span>@@ -60,6 +60,10 @@</span><br><span> /* NRI bitlen and usable NULL-NRI ranges */</span><br><span> uint8_t nri_bitlen;</span><br><span> struct osmo_nri_ranges *null_nri_ranges;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Used for testing: If not NULL then this SGSN is returned by</span><br><span style="color: hsl(120, 100%, 40%);">+ * gbproxy_sgsn_by_tlli() */</span><br><span style="color: hsl(120, 100%, 40%);">+ struct gbproxy_sgsn *nsf_override;</span><br><span> } pool;</span><br><span> </span><br><span> /* hash table of all BSS side Gb peers */</span><br><span>@@ -229,5 +233,7 @@</span><br><span> struct gbproxy_sgsn *gbproxy_sgsn_by_nsei(struct gbproxy_config *cfg, uint16_t nsei);</span><br><span> struct gbproxy_sgsn *gbproxy_sgsn_by_nsei_or_new(struct gbproxy_config *cfg, uint16_t nsei);</span><br><span> struct gbproxy_sgsn *gbproxy_sgsn_by_nri(struct gbproxy_config *cfg, uint16_t nri, bool *null_nri);</span><br><span style="color: hsl(120, 100%, 40%);">+struct gbproxy_sgsn *gbproxy_sgsn_by_tlli(struct gbproxy_config *cfg, struct gbproxy_sgsn *sgsn_avoid,</span><br><span style="color: hsl(120, 100%, 40%);">+ uint32_t tlli);</span><br><span> </span><br><span> #endif</span><br><span>diff --git a/src/gbproxy/gb_proxy.c b/src/gbproxy/gb_proxy.c</span><br><span>index 4b6dc09..bce4055 100644</span><br><span>--- a/src/gbproxy/gb_proxy.c</span><br><span>+++ b/src/gbproxy/gb_proxy.c</span><br><span>@@ -20,6 +20,7 @@</span><br><span> *</span><br><span> */</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+#include "osmocom/vty/command.h"</span><br><span> #include <unistd.h></span><br><span> #include <stdio.h></span><br><span> #include <stdlib.h></span><br><span>@@ -38,6 +39,7 @@</span><br><span> #include <osmocom/core/select.h></span><br><span> #include <osmocom/core/rate_ctr.h></span><br><span> #include <osmocom/core/stats.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <osmocom/core/utils.h></span><br><span> </span><br><span> #include <osmocom/gprs/gprs_ns2.h></span><br><span> #include <osmocom/gprs/gprs_bssgp.h></span><br><span>@@ -45,6 +47,7 @@</span><br><span> #include <osmocom/gprs/gprs_bssgp_bss.h></span><br><span> #include <osmocom/gprs/bssgp_bvc_fsm.h></span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+#include <osmocom/gsm/gsm23236.h></span><br><span> #include <osmocom/gsm/gsm_utils.h></span><br><span> </span><br><span> #include <osmocom/sgsn/signal.h></span><br><span>@@ -201,42 +204,131 @@</span><br><span> * PTP BVC handling</span><br><span> ***********************************************************************/</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-/* route an uplink message on a PTP-BVC to a SGSN using the TLLI */</span><br><span style="color: hsl(0, 100%, 40%);">-static int gbprox_bss2sgsn_tlli(struct gbproxy_cell *cell, struct msgb *msg, uint32_t tlli,</span><br><span style="color: hsl(120, 100%, 40%);">+/* FIXME: Handle the tlli NULL case correctly,</span><br><span style="color: hsl(120, 100%, 40%);">+ * This function should take a generic selector</span><br><span style="color: hsl(120, 100%, 40%);">+ * and choose an sgsn based on that</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static struct gbproxy_sgsn *gbproxy_select_sgsn(struct gbproxy_config *cfg, const uint32_t *tlli)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct gbproxy_sgsn *sgsn = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct gbproxy_sgsn *sgsn_avoid = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ int tlli_type;</span><br><span style="color: hsl(120, 100%, 40%);">+ int16_t nri;</span><br><span style="color: hsl(120, 100%, 40%);">+ bool null_nri = false;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!tlli) {</span><br><span style="color: hsl(120, 100%, 40%);">+ sgsn = llist_first_entry(&cfg->sgsns, struct gbproxy_sgsn, list);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!sgsn) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ LOGPSGSN(sgsn, LOGL_INFO, "Could not get TLLI, using first SGSN\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ return sgsn;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (cfg->pool.nri_bitlen == 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Pooling is disabled */</span><br><span style="color: hsl(120, 100%, 40%);">+ sgsn = llist_first_entry(&cfg->sgsns, struct gbproxy_sgsn, list);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!sgsn) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ LOGPSGSN(sgsn, LOGL_INFO, "Pooling disabled, using first configured SGSN\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Pooling is enabled, try to use the NRI for routing to an SGSN</span><br><span style="color: hsl(120, 100%, 40%);">+ * See 3GPP TS 23.236 Ch. 5.3.2 */</span><br><span style="color: hsl(120, 100%, 40%);">+ tlli_type = gprs_tlli_type(*tlli);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (tlli_type == TLLI_LOCAL || tlli_type == TLLI_FOREIGN) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Only get/use the NRI if tlli type is local */</span><br><span style="color: hsl(120, 100%, 40%);">+ osmo_tmsi_nri_v_get(&nri, *tlli, cfg->pool.nri_bitlen);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (nri >= 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Get the SGSN for the NRI */</span><br><span style="color: hsl(120, 100%, 40%);">+ sgsn = gbproxy_sgsn_by_nri(cfg, nri, &null_nri);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (sgsn && !null_nri)</span><br><span style="color: hsl(120, 100%, 40%);">+ return sgsn;</span><br><span style="color: hsl(120, 100%, 40%);">+ /* If the NRI is the null NRI, we need to avoid the chosen SGSN */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (null_nri && sgsn) {</span><br><span style="color: hsl(120, 100%, 40%);">+ sgsn_avoid = sgsn;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* We couldn't get the NRI from the TLLI */</span><br><span style="color: hsl(120, 100%, 40%);">+ LOGP(DGPRS, LOGL_ERROR, "Could not extract NRI from local TLLI %u\n", *tlli);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* If we haven't found an SGSN yet we need to choose one, but avoid the one in sgsn_avoid</span><br><span style="color: hsl(120, 100%, 40%);">+ * NOTE: This function is not stable if the number of SGSNs or allow_attach changes</span><br><span style="color: hsl(120, 100%, 40%);">+ * We could implement TLLI tracking here, but 3GPP TS 23.236 Ch. 5.3.2 (see NOTE) argues that</span><br><span style="color: hsl(120, 100%, 40%);">+ * we can just wait for the MS to reattempt the procedure.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!sgsn)</span><br><span style="color: hsl(120, 100%, 40%);">+ sgsn = gbproxy_sgsn_by_tlli(cfg, sgsn_avoid, *tlli);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!sgsn) {</span><br><span style="color: hsl(120, 100%, 40%);">+ LOGP(DGPRS, LOGL_ERROR, "No suitable SGSN found for TLLI %u\n", *tlli);</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return sgsn;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! Find the correct gbproxy_bvc given a cell and an SGSN</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] cfg The gbproxy configuration</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] cell The cell the message belongs to</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] tlli An optional TLLI used for tracking</span><br><span style="color: hsl(120, 100%, 40%);">+ * \return Returns 0 on success, otherwise a negative value</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static struct gbproxy_bvc *gbproxy_select_sgsn_bvc(struct gbproxy_config *cfg, struct gbproxy_cell *cell, const uint32_t *tlli)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct gbproxy_sgsn *sgsn;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct gbproxy_bvc *sgsn_bvc = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ sgsn = gbproxy_select_sgsn(cfg, tlli);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!sgsn) {</span><br><span style="color: hsl(120, 100%, 40%);">+ LOGPCELL(cell, LOGL_ERROR, "Could not find any SGSN, dropping message!\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Get the BVC for this SGSN/NSE */</span><br><span style="color: hsl(120, 100%, 40%);">+ for (int i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {</span><br><span style="color: hsl(120, 100%, 40%);">+ sgsn_bvc = cell->sgsn_bvc[i];</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!sgsn_bvc)</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (sgsn->nse != sgsn_bvc->nse)</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return sgsn_bvc;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* This shouldn't happen */</span><br><span style="color: hsl(120, 100%, 40%);">+ LOGPCELL(cell, LOGL_ERROR, "Could not find matching BVC for SGSN, dropping message!\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! Send a message to the next SGSN, possibly ignoring the null SGSN</span><br><span style="color: hsl(120, 100%, 40%);">+ * route an uplink message on a PTP-BVC to a SGSN using the TLLI</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] cell The cell the message belongs to</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] msg The BSSGP message</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] null_sgsn If not NULL then avoid this SGSN (because this message contains its null NRI)</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] tlli An optional TLLI used for tracking</span><br><span style="color: hsl(120, 100%, 40%);">+ * \return Returns 0 on success, otherwise a negative value</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static int gbprox_bss2sgsn_tlli(struct gbproxy_cell *cell, struct msgb *msg, const uint32_t *tlli,</span><br><span> bool sig_bvci)</span><br><span> {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct gbproxy_config *cfg = cell->cfg;</span><br><span> struct gbproxy_bvc *sgsn_bvc;</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int i;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- /* FIXME: derive NRI from TLLI */</span><br><span style="color: hsl(0, 100%, 40%);">- /* FIXME: find the SGSN for that NRI */</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* HACK: we currently simply pick the first SGSN we find */</span><br><span style="color: hsl(0, 100%, 40%);">- for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {</span><br><span style="color: hsl(0, 100%, 40%);">- sgsn_bvc = cell->sgsn_bvc[i];</span><br><span style="color: hsl(0, 100%, 40%);">- if (sgsn_bvc)</span><br><span style="color: hsl(0, 100%, 40%);">- return gbprox_relay2peer(msg, sgsn_bvc, sig_bvci ? 0 : sgsn_bvc->bvci);</span><br><span style="color: hsl(120, 100%, 40%);">+ sgsn_bvc = gbproxy_select_sgsn_bvc(cfg, cell, tlli);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!sgsn_bvc) {</span><br><span style="color: hsl(120, 100%, 40%);">+ LOGPCELL(cell, LOGL_NOTICE, "Could not find any SGSN for TLLI %u, dropping message!\n", *tlli);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -EINVAL;</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return gbprox_relay2peer(msg, sgsn_bvc, sig_bvci ? 0 : sgsn_bvc->bvci);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-static int gbprox_bss2sgsn_null_nri(struct gbproxy_cell *cell, struct msgb *msg)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- struct gbproxy_bvc *sgsn_bvc;</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int i;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* FIXME: find the SGSN for that NRI */</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* HACK: we currently simply pick the first SGSN we find */</span><br><span style="color: hsl(0, 100%, 40%);">- for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {</span><br><span style="color: hsl(0, 100%, 40%);">- sgsn_bvc = cell->sgsn_bvc[i];</span><br><span style="color: hsl(0, 100%, 40%);">- if (sgsn_bvc)</span><br><span style="color: hsl(0, 100%, 40%);">- return gbprox_relay2peer(msg, sgsn_bvc, sgsn_bvc->bvci);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> /* Receive an incoming PTP message from a BSS-side NS-VC */</span><br><span> static int gbprox_rx_ptp_from_bss(struct gbproxy_nse *nse, struct msgb *msg, uint16_t ns_bvci)</span><br><span> {</span><br><span>@@ -314,18 +406,20 @@</span><br><span> case BSSGP_PDUT_PS_HO_CANCEL:</span><br><span> /* We can route based on TLLI-NRI */</span><br><span> tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI));</span><br><span style="color: hsl(0, 100%, 40%);">- rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, tlli, false);</span><br><span style="color: hsl(120, 100%, 40%);">+ rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, &tlli, false);</span><br><span> break;</span><br><span> case BSSGP_PDUT_RADIO_STATUS:</span><br><span> if (TLVP_PRESENT(&tp, BSSGP_IE_TLLI)) {</span><br><span> tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI));</span><br><span style="color: hsl(0, 100%, 40%);">- rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, tlli, false);</span><br><span style="color: hsl(120, 100%, 40%);">+ rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, &tlli, false);</span><br><span> } else if (TLVP_PRESENT(&tp, BSSGP_IE_TMSI)) {</span><br><span> /* we treat the TMSI like a TLLI and extract the NRI from it */</span><br><span> tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TMSI));</span><br><span style="color: hsl(0, 100%, 40%);">- rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, tlli, false);</span><br><span style="color: hsl(120, 100%, 40%);">+ rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, &tlli, false);</span><br><span> } else if (TLVP_PRESENT(&tp, BSSGP_IE_IMSI)) {</span><br><span style="color: hsl(0, 100%, 40%);">- rc = gbprox_bss2sgsn_null_nri(bss_bvc->cell, msg);</span><br><span style="color: hsl(120, 100%, 40%);">+ // FIXME: Use the IMSI as selector?</span><br><span style="color: hsl(120, 100%, 40%);">+ rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, NULL, false);</span><br><span style="color: hsl(120, 100%, 40%);">+ //rc = gbprox_bss2sgsn_hashed(bss_bvc->cell, msg, NULL);</span><br><span> } else</span><br><span> LOGPBVC(bss_bvc, LOGL_ERROR, "Rx RADIO-STATUS without any of the conditional IEs\n");</span><br><span> break;</span><br><span>@@ -828,7 +922,7 @@</span><br><span> from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);</span><br><span> if (!from_bvc)</span><br><span> goto err_no_bvc;</span><br><span style="color: hsl(0, 100%, 40%);">- gbprox_bss2sgsn_tlli(from_bvc->cell, msg, tlli, true);</span><br><span style="color: hsl(120, 100%, 40%);">+ gbprox_bss2sgsn_tlli(from_bvc->cell, msg, &tlli, true);</span><br><span> break;</span><br><span> default:</span><br><span> LOGPNSE(nse, LOGL_ERROR, "Rx %s: Implementation missing\n", pdut_name);</span><br><span>@@ -1288,7 +1382,6 @@</span><br><span> /* by default we advertise 100% of the BSS-side capacity to _each_ SGSN */</span><br><span> cfg->pool.bvc_fc_ratio = 100;</span><br><span> cfg->pool.null_nri_ranges = osmo_nri_ranges_alloc(cfg);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> hash_init(cfg->bss_nses);</span><br><span> hash_init(cfg->sgsn_nses);</span><br><span> hash_init(cfg->cells);</span><br><span>diff --git a/src/gbproxy/gb_proxy_main.c b/src/gbproxy/gb_proxy_main.c</span><br><span>index e85e951..c660ede 100644</span><br><span>--- a/src/gbproxy/gb_proxy_main.c</span><br><span>+++ b/src/gbproxy/gb_proxy_main.c</span><br><span>@@ -303,6 +303,8 @@</span><br><span> </span><br><span> gprs_ns2_vty_create();</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ /* TODO: Warn if we create a gbproxy_nse for an NSEI which we don't have a bind */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /* start telnet after reading config for vty_get_bind_addr() */</span><br><span> rc = telnet_init_dynif(tall_sgsn_ctx, NULL,</span><br><span> vty_get_bind_addr(), OSMO_VTY_PORT_GBPROXY);</span><br><span>diff --git a/src/gbproxy/gb_proxy_peer.c b/src/gbproxy/gb_proxy_peer.c</span><br><span>index 863ec50..3b12046 100644</span><br><span>--- a/src/gbproxy/gb_proxy_peer.c</span><br><span>+++ b/src/gbproxy/gb_proxy_peer.c</span><br><span>@@ -343,7 +343,15 @@</span><br><span> return nse;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-/* SGSN */</span><br><span style="color: hsl(120, 100%, 40%);">+/***********************************************************************</span><br><span style="color: hsl(120, 100%, 40%);">+ * SGSN - Serving GPRS Support Node</span><br><span style="color: hsl(120, 100%, 40%);">+ ***********************************************************************/</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! Allocate a new SGSN. This ensures the corresponding gbproxy_nse is allocated as well</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] cfg The gbproxy configuration</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] nsei The nsei where the SGSN can be reached</span><br><span style="color: hsl(120, 100%, 40%);">+ * \return The SGSN, NULL if it couldn't be allocated</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span> struct gbproxy_sgsn *gbproxy_sgsn_alloc(struct gbproxy_config *cfg, uint16_t nsei)</span><br><span> {</span><br><span> struct gbproxy_sgsn *sgsn;</span><br><span>@@ -381,6 +389,9 @@</span><br><span> talloc_free(sgsn);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*! Free the SGSN. This ensures the corresponding gbproxy_nse is freed as well</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] sgsn The SGSN</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span> void gbproxy_sgsn_free(struct gbproxy_sgsn *sgsn)</span><br><span> {</span><br><span> if (!sgsn)</span><br><span>@@ -392,6 +403,11 @@</span><br><span> _sgsn_free(sgsn);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*! Return the SGSN for a given NSEI</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] cfg The gbproxy configuration</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] nsei The nsei where the SGSN can be reached</span><br><span style="color: hsl(120, 100%, 40%);">+ * \return Returns the matching SGSN or NULL if it couldn't be found</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span> struct gbproxy_sgsn *gbproxy_sgsn_by_nsei(struct gbproxy_config *cfg, uint16_t nsei)</span><br><span> {</span><br><span> struct gbproxy_sgsn *sgsn;</span><br><span>@@ -405,6 +421,11 @@</span><br><span> return NULL;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*! Return the SGSN for a given NSEI, creating a new one if none exists</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] cfg The gbproxy configuration</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] nsei The nsei where the SGSN can be reached</span><br><span style="color: hsl(120, 100%, 40%);">+ * \return Returns the SGSN</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span> struct gbproxy_sgsn *gbproxy_sgsn_by_nsei_or_new(struct gbproxy_config *cfg, uint16_t nsei)</span><br><span> {</span><br><span> struct gbproxy_sgsn *sgsn;</span><br><span>@@ -443,3 +464,47 @@</span><br><span> </span><br><span> return NULL;</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! Seleect a pseudo-random SGSN for a given TLLI, ignoring any SGSN that is not accepting connections</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] cfg The gbproxy configuration</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] sgsn_avoid If not NULL then avoid this SGSN when selecting a new one. Use for load redistribution</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] tlli The tlli to choose an SGSN for. The same tlli will map to the same SGSN as long as no SGSN is</span><br><span style="color: hsl(120, 100%, 40%);">+ added/removed or allow_attach changes.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \return Returns the sgsn on success, NULL if no SGSN that allows new connections could be found</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+struct gbproxy_sgsn *gbproxy_sgsn_by_tlli(struct gbproxy_config *cfg, struct gbproxy_sgsn *sgsn_avoid,</span><br><span style="color: hsl(120, 100%, 40%);">+ uint32_t tlli)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ uint32_t i = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ uint32_t index, num_sgsns;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct gbproxy_sgsn *sgsn;</span><br><span style="color: hsl(120, 100%, 40%);">+ OSMO_ASSERT(cfg);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ // TODO: We should keep track of count in cfg</span><br><span style="color: hsl(120, 100%, 40%);">+ num_sgsns = llist_count(&cfg->sgsns);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (num_sgsns == 0)</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ // FIXME: 256 SGSNs ought to be enough for everyone</span><br><span style="color: hsl(120, 100%, 40%);">+ index = hash_32(tlli, 8) % num_sgsns;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ // Get the first enabled SGSN after index</span><br><span style="color: hsl(120, 100%, 40%);">+ llist_for_each_entry(sgsn, &cfg->sgsns, list) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (i >= index && sgsn->pool.allow_attach) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return sgsn;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ i++;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ // Start again from the beginning</span><br><span style="color: hsl(120, 100%, 40%);">+ llist_for_each_entry(sgsn, &cfg->sgsns, list) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (i > index) {</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (sgsn->pool.allow_attach) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return sgsn;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ i++;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/c/osmo-sgsn/+/21881">change 21881</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/c/osmo-sgsn/+/21881"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: osmo-sgsn </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I58b9f55065f6bd43450e4b07cffe7ba132b1fd9b </div>
<div style="display:none"> Gerrit-Change-Number: 21881 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: daniel <dwillmann@sysmocom.de> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>