gbproxy: Add new VTY-managed timer: link-list clean-stale-timer

This timer allows periodically cleaning up stale links in link-list of
each gbproxy_peer. Previous to this patch, this kind of cleanup
(gbproxy_remove_stale_link_infos) was being done only as a consequence
of external events being triggered, such as a message from that peer
being received.
It was found in a production network agreggating several BSS that some
of them were offline for a longtime but gbproxy was still caching big
amounts of really old link_info for the NSEI assigned to those BSS,
because since they were probably turned off abruptely, no new messages
were received from it which would trigger the cleanup.
As a consequence, it has been observed that a timer to periodically
clean up old entries (link-list max-age) is requird in case w don't
receive messages from that NSEI periodically.

Related: SYS#4431
Change-Id: Ic777016f6d4f0e30fb736484774ca46878f17b7a
diff --git a/src/gprs/gb_proxy_vty.c b/src/gprs/gb_proxy_vty.c
index d743355..52c39fd 100644
--- a/src/gprs/gb_proxy_vty.c
+++ b/src/gprs/gb_proxy_vty.c
@@ -120,6 +120,9 @@
 		vty_out(vty, " secondary-sgsn nsei %u%s", g_cfg->nsip_sgsn2_nsei,
 			VTY_NEWLINE);
 
+	if (g_cfg->clean_stale_timer_freq > 0)
+		vty_out(vty, " link-list clean-stale-timer %u%s",
+			g_cfg->clean_stale_timer_freq, VTY_NEWLINE);
 	if (g_cfg->tlli_max_age > 0)
 		vty_out(vty, " link-list max-age %d%s",
 			g_cfg->tlli_max_age, VTY_NEWLINE);
@@ -407,6 +410,44 @@
 
 #define GBPROXY_LINK_LIST_STR "Set TLLI list parameters\n"
 #define GBPROXY_LINK_STR "Set TLLI parameters\n"
+
+#define GBPROXY_CLEAN_STALE_TIMER_STR "Periodic timer to clean stale links\n"
+
+DEFUN(cfg_gbproxy_link_list_clean_stale_timer,
+      cfg_gbproxy_link_list_clean_stale_timer_cmd,
+      "link-list clean-stale-timer <1-999999>",
+      GBPROXY_LINK_LIST_STR GBPROXY_CLEAN_STALE_TIMER_STR
+      "Frequency at which the periodic timer is fired (in seconds)\n")
+{
+	struct gbproxy_peer *peer;
+	g_cfg->clean_stale_timer_freq = (unsigned int) atoi(argv[0]);
+
+	/* Re-schedule running timers soon in case prev frequency was really big
+	   and new frequency is desired to be lower. After initial run, periodic
+	   time is used. Use random() to avoid firing timers for all peers at
+	   the same time */
+	llist_for_each_entry(peer, &g_cfg->bts_peers, list)
+		osmo_timer_schedule(&peer->clean_stale_timer,
+					random() % 5, random() % 1000000);
+
+	return CMD_SUCCESS;
+}
+
+DEFUN(cfg_gbproxy_link_list_no_clean_stale_timer,
+      cfg_gbproxy_link_list_no_clean_stale_timer_cmd,
+      "no link-list clean-stale-timer",
+      NO_STR GBPROXY_LINK_LIST_STR GBPROXY_CLEAN_STALE_TIMER_STR)
+
+{
+	struct gbproxy_peer *peer;
+	g_cfg->clean_stale_timer_freq = 0;
+
+	llist_for_each_entry(peer, &g_cfg->bts_peers, list)
+		osmo_timer_del(&peer->clean_stale_timer);
+
+	return CMD_SUCCESS;
+}
+
 #define GBPROXY_MAX_AGE_STR "Limit maximum age\n"
 
 DEFUN(cfg_gbproxy_link_list_max_age,
@@ -846,6 +887,7 @@
 	install_element(GBPROXY_NODE, &cfg_gbproxy_secondary_sgsn_cmd);
 	install_element(GBPROXY_NODE, &cfg_gbproxy_patch_ptmsi_cmd);
 	install_element(GBPROXY_NODE, &cfg_gbproxy_acquire_imsi_cmd);
+	install_element(GBPROXY_NODE, &cfg_gbproxy_link_list_clean_stale_timer_cmd);
 	install_element(GBPROXY_NODE, &cfg_gbproxy_link_list_max_age_cmd);
 	install_element(GBPROXY_NODE, &cfg_gbproxy_link_list_max_len_cmd);
 	install_element(GBPROXY_NODE, &cfg_gbproxy_link_list_keep_mode_cmd);
@@ -857,6 +899,7 @@
 	install_element(GBPROXY_NODE, &cfg_gbproxy_no_secondary_sgsn_cmd);
 	install_element(GBPROXY_NODE, &cfg_gbproxy_no_patch_ptmsi_cmd);
 	install_element(GBPROXY_NODE, &cfg_gbproxy_no_acquire_imsi_cmd);
+	install_element(GBPROXY_NODE, &cfg_gbproxy_link_list_no_clean_stale_timer_cmd);
 	install_element(GBPROXY_NODE, &cfg_gbproxy_link_list_no_max_age_cmd);
 	install_element(GBPROXY_NODE, &cfg_gbproxy_link_list_no_max_len_cmd);
 	install_element(GBPROXY_NODE, &cfg_gbproxy_link_no_stored_msgs_max_len_cmd);