tbf: Use a hysteresis when discarding DL LLC frames

Currently single LLC blocks are discarded when the PDU lifetime
expires. If an IP packet has been fragmented either on the IP or on
the LLC layer and is therefore distributed over several LLC frames,
the kept fragments are transmitted and then discarded by the MS
because of the missing PDU. This can cause massive IP packet loss
when there are many fragmented packets (e.g. when trying 'ping
-s1800' or if the GGSN chops downlink IP packets into several SNDCP
packets).

On the other hand, discarding too many packets might disturb the
congestion handling of TCP. Dropping plain TCP ACKs might also hinder
flow control and congestion avoidance.

This commit adds a hysteresis algorithm to the LLC discard loop. If
an LLC message's age reaches the high water mark, further message's
with an age above the low water mark are discarded, too. This is
aborted, if a GMM, a non-UI, or a small message is detected. In
these cases, that message is kept.

The following VTY commands are added (pcu config node):

- queue hysteresis <1-65535>   set the difference between high
                               (lifetime) and low watermark in
                               centiseconds
- no queue hysteresis          disable this feature (default)

Since the SGSN will most probably send all fragments of a single
N-PDU without much delay between them, a value slightly above the
average transmission delay jitter between SGSN and PCU is probably a
sensible value to discard all fragments of a single IP packet.

This is an experimental feature that might be replaced by more
advanced means of active queue management in the future.

Sponsored-by: On-Waves ehf
diff --git a/src/tbf_dl.cpp b/src/tbf_dl.cpp
index 7e0883b..aeec23f 100644
--- a/src/tbf_dl.cpp
+++ b/src/tbf_dl.cpp
@@ -209,30 +209,58 @@
 struct msgb *gprs_rlcmac_dl_tbf::llc_dequeue(bssgp_bvc_ctx *bctx)
 {
 	struct msgb *msg;
-	struct timeval *tv, tv_now;
+	struct timeval *tv, tv_now, tv_now2;
 	uint32_t octets = 0, frames = 0;
+	struct timeval hyst_delta = {0, 0};
+	const unsigned keep_small_thresh = 60;
+
+	if (bts_data()->llc_discard_csec)
+		csecs_to_timeval(bts_data()->llc_discard_csec, &hyst_delta);
 
 	gettimeofday(&tv_now, NULL);
+	timeradd(&tv_now, &hyst_delta, &tv_now2);
 
 	while ((msg = m_llc.dequeue())) {
 		tv = (struct timeval *)msg->data;
 		msgb_pull(msg, sizeof(*tv));
 		msgb_pull(msg, sizeof(*tv));
 
-		if (gprs_llc::is_frame_expired(&tv_now, tv)) {
-			LOGP(DRLCMACDL, LOGL_NOTICE, "%s Discarding LLC PDU "
-				"because lifetime limit reached. Queue size %zu\n",
-				tbf_name(this), m_llc.m_queue_size);
-			bts->llc_timedout_frame();
-			frames++;
-			octets += msg->len;
-			msgb_free(msg);
-			continue;
+		/* Is the age below the low water mark? */
+		if (!gprs_llc::is_frame_expired(&tv_now2, tv))
+			break;
+
+		/* Is the age below the high water mark */
+		if (!gprs_llc::is_frame_expired(&tv_now, tv)) {
+			/* Has the previous message not been dropped? */
+			if (frames == 0)
+				break;
+
+			/* Hysteresis mode, try to discard LLC messages until
+			 * the low water mark has been reached */
+
+			/* Check whether to abort the hysteresis mode */
+
+			/* Is the frame small, perhaps only a TCP ACK? */
+			if (msg->len <= keep_small_thresh)
+				break;
+
+			/* Is it a GMM message? */
+			if (!gprs_llc::is_user_data_frame(msg->data, msg->len))
+				break;
 		}
-		break;
+
+		bts->llc_timedout_frame();
+		frames++;
+		octets += msg->len;
+		msgb_free(msg);
+		continue;
 	}
 
 	if (frames) {
+		LOGP(DRLCMACDL, LOGL_NOTICE, "%s Discarding LLC PDU "
+			"because lifetime limit reached, "
+			"count=%u new_queue_size=%zu\n",
+			tbf_name(this), frames, m_llc.m_queue_size);
 		if (frames > 0xff)
 			frames = 0xff;
 		if (octets > 0xffffff)