gbproxy: Implement scaling of BVC flow control in SGSN pool

When there are multiple SGSNs inside a pool, we need to decide
how much of the per-BVC capacity advertised by the BSS in its
BVC-FLOW-CONTROL we should announce to each of the pool members.

A conservative approach would be to advertise 1/num_sgsn, but
there may also be use case where over-provisioning (announcing more
than an equal share of the capacity) is useful.

Hence, let's introduce "pool bvc-flow-control-ratio <1-100>" in order
to allow the administrator to decide.

Related: OS#4891
Change-Id: Ibe5addf657e7237499ca0205bacfe999ecd1e771
diff --git a/src/gbproxy/gb_proxy.c b/src/gbproxy/gb_proxy.c
index a976d25..53aecdf 100644
--- a/src/gbproxy/gb_proxy.c
+++ b/src/gbproxy/gb_proxy.c
@@ -40,6 +40,7 @@
 
 #include <osmocom/gprs/gprs_ns2.h>
 #include <osmocom/gprs/gprs_bssgp.h>
+#include <osmocom/gprs/gprs_bssgp2.h>
 #include <osmocom/gprs/gprs_bssgp_bss.h>
 #include <osmocom/gprs/bssgp_bvc_fsm.h>
 
@@ -566,15 +567,28 @@
 /* BVC FSM informs us about BVC-FC PDU receive */
 static void bss_ptp_bvc_fc_bvc(uint16_t nsei, uint16_t bvci, const struct bssgp2_flow_ctrl *fc, void *priv)
 {
+	struct bssgp2_flow_ctrl fc_reduced;
 	struct gbproxy_bvc *bss_bvc = priv;
-	struct gbproxy_cell *cell = bss_bvc->cell;
+	struct gbproxy_cell *cell;
+	struct gbproxy_config *cfg;
 
+	OSMO_ASSERT(bss_bvc);
+	OSMO_ASSERT(fc);
+
+	cell = bss_bvc->cell;
 	if (!cell)
 		return;
 
-	/* FIXME: actually split the bandwidth among the SGSNs! */
+	cfg = cell->cfg;
 
-	dispatch_to_all_sgsn_bvc(cell, BSSGP_BVCFSM_E_REQ_FC_BVC, (void *) fc);
+	/* reduce / scale according to configuration to make sure we only advertise a fraction
+	 * of the capacity to each of the SGSNs in the pool */
+	fc_reduced = *fc;
+	fc_reduced.bucket_size_max = (fc->bucket_size_max * cfg->pool.bvc_fc_ratio) / 100;
+	fc_reduced.bucket_leak_rate = (fc->bucket_leak_rate * cfg->pool.bvc_fc_ratio) / 100;
+	/* we don't modify the per-MS related values as any single MS is only served by one SGSN */
+
+	dispatch_to_all_sgsn_bvc(cell, BSSGP_BVCFSM_E_REQ_FC_BVC, (void *) &fc_reduced);
 }
 
 static const struct bssgp_bvc_fsm_ops bss_ptp_bvc_fsm_ops = {
@@ -1270,6 +1284,8 @@
 {
 	struct timespec tp;
 
+	/* by default we advertise 100% of the BSS-side capacity to _each_ SGSN */
+	cfg->pool.bvc_fc_ratio = 100;
 	hash_init(cfg->bss_nses);
 	cfg->ctrg = rate_ctr_group_alloc(tall_sgsn_ctx, &global_ctrg_desc, 0);
 	if (!cfg->ctrg) {
diff --git a/src/gbproxy/gb_proxy_vty.c b/src/gbproxy/gb_proxy_vty.c
index a9db596..49ec91c 100644
--- a/src/gbproxy/gb_proxy_vty.c
+++ b/src/gbproxy/gb_proxy_vty.c
@@ -76,6 +76,9 @@
 
 	vty_out(vty, "gbproxy%s", VTY_NEWLINE);
 
+	if (g_cfg->pool.bvc_fc_ratio != 100)
+		vty_out(vty, " pool bvc-flow-control-ratio %u%s", g_cfg->pool.bvc_fc_ratio, VTY_NEWLINE);
+
 	hash_for_each(g_cfg->sgsn_nses, i, nse, list) {
 		vty_out(vty, " sgsn nsei %u%s", nse->nsei, VTY_NEWLINE);
 	}
@@ -134,6 +137,17 @@
 	return CMD_WARNING;
 }
 
+DEFUN(cfg_pool_bvc_fc_ratio,
+      cfg_pool_bvc_fc_ratio_cmd,
+      "pool bvc-flow-control-ratio <1-100>",
+      "SGSN Pool related configuration\n"
+      "Ratio of BSS-advertised bucket size + leak rate advertised to each SGSN\n"
+      "Ratio of BSS-advertised bucket size + leak rate advertised to each SGSN (Percent)\n")
+{
+	g_cfg->pool.bvc_fc_ratio = atoi(argv[0]);
+	return CMD_SUCCESS;
+}
+
 static void log_set_bvc_filter(struct log_target *target,
 				const uint16_t *bvci)
 {
@@ -315,6 +329,7 @@
 	install_element(CONFIG_NODE, &cfg_gbproxy_cmd);
 	install_node(&gbproxy_node, config_write_gbproxy);
 	install_element(GBPROXY_NODE, &cfg_nsip_sgsn_nsei_cmd);
+	install_element(GBPROXY_NODE, &cfg_pool_bvc_fc_ratio_cmd);
 
 	return 0;
 }