Merge branch 'master' of nehalam:src/iproute2
diff --git a/tc/q_choke.c b/tc/q_choke.c
index 7816f62..c616926 100644
--- a/tc/q_choke.c
+++ b/tc/q_choke.c
@@ -18,6 +18,7 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <string.h>
+#include <math.h>
 
 #include "utils.h"
 #include "tc_util.h"
@@ -41,6 +42,7 @@
 	int ecn_ok = 0;
 	int wlog;
 	__u8 sbuf[256];
+	__u32 max_P;
 	struct rtattr *tail;
 
 	memset(&opt, 0, sizeof(opt));
@@ -156,25 +158,31 @@
 	addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
 	addattr_l(n, 1024, TCA_CHOKE_PARMS, &opt, sizeof(opt));
 	addattr_l(n, 1024, TCA_CHOKE_STAB, sbuf, 256);
+	max_P = probability * pow(2, 32);
+	addattr_l(n, 1024, TCA_CHOKE_MAX_P, &max_P, sizeof(max_P));
 	tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
 	return 0;
 }
 
 static int choke_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
 {
-	struct rtattr *tb[TCA_CHOKE_STAB+1];
+	struct rtattr *tb[TCA_CHOKE_MAX+1];
 	const struct tc_red_qopt *qopt;
+	__u32 max_P = 0;
 
 	if (opt == NULL)
 		return 0;
 
-	parse_rtattr_nested(tb, TCA_CHOKE_STAB, opt);
+	parse_rtattr_nested(tb, TCA_CHOKE_MAX, opt);
 
 	if (tb[TCA_CHOKE_PARMS] == NULL)
 		return -1;
 	qopt = RTA_DATA(tb[TCA_CHOKE_PARMS]);
 	if (RTA_PAYLOAD(tb[TCA_CHOKE_PARMS])  < sizeof(*qopt))
 		return -1;
+	if (tb[TCA_CHOKE_MAX_P] &&
+	    RTA_PAYLOAD(tb[TCA_CHOKE_MAX_P]) >= sizeof(__u32))
+		max_P = *(__u32 *)RTA_DATA(tb[TCA_CHOKE_MAX_P]);
 
 	fprintf(f, "limit %up min %up max %up ",
 		qopt->limit, qopt->qth_min, qopt->qth_max);
@@ -183,8 +191,12 @@
 		fprintf(f, "ecn ");
 
 	if (show_details) {
-		fprintf(f, "ewma %u Plog %u Scell_log %u",
-			qopt->Wlog, qopt->Plog, qopt->Scell_log);
+		fprintf(f, "ewma %u ", qopt->Wlog);
+		if (max_P)
+			fprintf(f, "probability %g ", max_P / pow(2, 32));
+		else
+			fprintf(f, "Plog %u ", qopt->Plog);
+		fprintf(f, "Scell_log %u", qopt->Scell_log);
 	}
 	return 0;
 }
diff --git a/tc/q_gred.c b/tc/q_gred.c
index 5fa0cc7..a4df3a6 100644
--- a/tc/q_gred.c
+++ b/tc/q_gred.c
@@ -21,6 +21,7 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <string.h>
+#include <math.h>
 
 #include "utils.h"
 #include "tc_util.h"
@@ -125,6 +126,7 @@
 	int wlog;
 	__u8 sbuf[256];
 	struct rtattr *tail;
+	__u32 max_P;
 
 	memset(&opt, 0, sizeof(opt));
 
@@ -251,14 +253,17 @@
 	addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
 	addattr_l(n, 1024, TCA_GRED_PARMS, &opt, sizeof(opt));
 	addattr_l(n, 1024, TCA_GRED_STAB, sbuf, 256);
+	max_P = probability * pow(2, 32);
+	addattr32(n, 1024, TCA_GRED_MAX_P, max_P);
 	tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
 	return 0;
 }
 
 static int gred_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
 {
-	struct rtattr *tb[TCA_GRED_STAB+1];
+	struct rtattr *tb[TCA_GRED_MAX + 1];
 	struct tc_gred_qopt *qopt;
+	__u32 *max_p = NULL;
 	int i;
 	SPRINT_BUF(b1);
 	SPRINT_BUF(b2);
@@ -269,11 +274,15 @@
 	if (opt == NULL)
 		return 0;
 
-	parse_rtattr_nested(tb, TCA_GRED_STAB, opt);
+	parse_rtattr_nested(tb, TCA_GRED_MAX, opt);
 
 	if (tb[TCA_GRED_PARMS] == NULL)
 		return -1;
 
+	if (tb[TCA_GRED_MAX_P] &&
+	    RTA_PAYLOAD(tb[TCA_GRED_MAX_P]) >= sizeof(__u32) * MAX_DPs)
+		max_p = RTA_DATA(tb[TCA_GRED_MAX_P]);
+
 	qopt = RTA_DATA(tb[TCA_GRED_PARMS]);
 	if (RTA_PAYLOAD(tb[TCA_GRED_PARMS])  < sizeof(*qopt)*MAX_DPs) {
 		fprintf(f,"\n GRED received message smaller than expected\n");
@@ -302,8 +311,12 @@
 				sprint_size(qopt->limit, b1),
 				sprint_size(qopt->qth_min, b2),
 				sprint_size(qopt->qth_max, b3));
-				fprintf(f, "ewma %u Plog %u Scell_log %u",
-				    qopt->Wlog, qopt->Plog, qopt->Scell_log);
+		fprintf(f, "ewma %u ", qopt->Wlog);
+		if (max_p)
+			fprintf(f, "probability %lg ", max_p[i] / pow(2, 32));
+		else
+			fprintf(f, "Plog %u ", qopt->Plog);
+		fprintf(f, "Scell_log %u", qopt->Scell_log);
 	}
 	return 0;
 }
diff --git a/tc/q_red.c b/tc/q_red.c
index 0e5d228..a4b5175 100644
--- a/tc/q_red.c
+++ b/tc/q_red.c
@@ -28,7 +28,7 @@
 static void explain(void)
 {
 	fprintf(stderr, "Usage: ... red limit BYTES [min BYTES] [max BYTES] avpkt BYTES [burst PACKETS]\n");
-	fprintf(stderr, "               [adaptative] [probability PROBABILITY] bandwidth KBPS\n");
+	fprintf(stderr, "               [adaptive] [probability PROBABILITY] bandwidth KBPS\n");
 	fprintf(stderr, "               [ecn] [harddrop]\n");
 }
 
@@ -95,6 +95,8 @@
 			opt.flags |= TC_RED_HARDDROP;
 		} else if (strcmp(*argv, "adaptative") == 0) {
 			opt.flags |= TC_RED_ADAPTATIVE;
+		} else if (strcmp(*argv, "adaptive") == 0) {
+			opt.flags |= TC_RED_ADAPTATIVE;
 		} else if (strcmp(*argv, "help") == 0) {
 			explain();
 			return -1;
@@ -184,7 +186,7 @@
 	if (qopt->flags & TC_RED_HARDDROP)
 		fprintf(f, "harddrop ");
 	if (qopt->flags & TC_RED_ADAPTATIVE)
-		fprintf(f, "adaptative ");
+		fprintf(f, "adaptive ");
 	if (show_details) {
 		fprintf(f, "ewma %u ", qopt->Wlog);
 		if (max_P)
diff --git a/tc/q_sfq.c b/tc/q_sfq.c
index 98ec530..96f63ff 100644
--- a/tc/q_sfq.c
+++ b/tc/q_sfq.c
@@ -19,56 +19,131 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <string.h>
+#include <math.h>
 
 #include "utils.h"
 #include "tc_util.h"
+#include "tc_red.h"
 
 static void explain(void)
 {
 	fprintf(stderr, "Usage: ... sfq [ limit NUMBER ] [ perturb SECS ] [ quantum BYTES ]\n");
-	fprintf(stderr, "               [ divisor NUMBER ]\n");
+	fprintf(stderr, "               [ divisor NUMBER ] [ flows NUMBER] [ depth NUMBER ]\n");
+	fprintf(stderr, "               [ headdrop ]\n");
+	fprintf(stderr, "               [ redflowlimit BYTES ] [ min BYTES ] [ max BYTES ]\n");
+	fprintf(stderr, "               [ avpkt BYTES ] [ burst PACKETS ] [ probability P ]\n");
+	fprintf(stderr, "               [ ecn ] [ harddrop ]\n");
 }
 
 static int sfq_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
 {
-	int ok=0;
-	struct tc_sfq_qopt opt;
+	int ok = 0, red = 0;
+	struct tc_sfq_qopt_v1 opt;
+	unsigned int burst = 0;
+	int wlog;
+	unsigned int avpkt = 1000;
+	double probability = 0.02;
 
 	memset(&opt, 0, sizeof(opt));
 
 	while (argc > 0) {
 		if (strcmp(*argv, "quantum") == 0) {
 			NEXT_ARG();
-			if (get_size(&opt.quantum, *argv)) {
+			if (get_size(&opt.v0.quantum, *argv)) {
 				fprintf(stderr, "Illegal \"limit\"\n");
 				return -1;
 			}
 			ok++;
 		} else if (strcmp(*argv, "perturb") == 0) {
 			NEXT_ARG();
-			if (get_integer(&opt.perturb_period, *argv, 0)) {
+			if (get_integer(&opt.v0.perturb_period, *argv, 0)) {
 				fprintf(stderr, "Illegal \"perturb\"\n");
 				return -1;
 			}
 			ok++;
 		} else if (strcmp(*argv, "limit") == 0) {
 			NEXT_ARG();
-			if (get_u32(&opt.limit, *argv, 0)) {
+			if (get_u32(&opt.v0.limit, *argv, 0)) {
 				fprintf(stderr, "Illegal \"limit\"\n");
 				return -1;
 			}
-			if (opt.limit < 2) {
+			if (opt.v0.limit < 2) {
 				fprintf(stderr, "Illegal \"limit\", must be > 1\n");
 				return -1;
 			}
 			ok++;
 		} else if (strcmp(*argv, "divisor") == 0) {
 			NEXT_ARG();
-			if (get_u32(&opt.divisor, *argv, 0)) {
+			if (get_u32(&opt.v0.divisor, *argv, 0)) {
 				fprintf(stderr, "Illegal \"divisor\"\n");
 				return -1;
 			}
 			ok++;
+		} else if (strcmp(*argv, "flows") == 0) {
+			NEXT_ARG();
+			if (get_u32(&opt.v0.flows, *argv, 0)) {
+				fprintf(stderr, "Illegal \"flows\"\n");
+				return -1;
+			}
+			ok++;
+		} else if (strcmp(*argv, "depth") == 0) {
+			NEXT_ARG();
+			if (get_u32(&opt.depth, *argv, 0)) {
+				fprintf(stderr, "Illegal \"flows\"\n");
+				return -1;
+			}
+			ok++;
+		} else if (strcmp(*argv, "headdrop") == 0) {
+			opt.headdrop = 1;
+			ok++;
+		} else if (strcmp(*argv, "redflowlimit") == 0) {
+			NEXT_ARG();
+			if (get_u32(&opt.limit, *argv, 0)) {
+				fprintf(stderr, "Illegal \"redflowlimit\"\n");
+				return -1;
+			}
+			red++;
+		} else if (strcmp(*argv, "min") == 0) {
+			NEXT_ARG();
+			if (get_u32(&opt.qth_min, *argv, 0)) {
+				fprintf(stderr, "Illegal \"min\"\n");
+				return -1;
+			}
+			red++;
+		} else if (strcmp(*argv, "max") == 0) {
+			NEXT_ARG();
+			if (get_u32(&opt.qth_max, *argv, 0)) {
+				fprintf(stderr, "Illegal \"max\"\n");
+				return -1;
+			}
+			red++;
+		} else if (strcmp(*argv, "burst") == 0) {
+			NEXT_ARG();
+			if (get_unsigned(&burst, *argv, 0)) {
+				fprintf(stderr, "Illegal \"burst\"\n");
+				return -1;
+			}
+			red++;
+		} else if (strcmp(*argv, "avpkt") == 0) {
+			NEXT_ARG();
+			if (get_size(&avpkt, *argv)) {
+				fprintf(stderr, "Illegal \"avpkt\"\n");
+				return -1;
+			}
+			red++;
+		} else if (strcmp(*argv, "probability") == 0) {
+			NEXT_ARG();
+			if (sscanf(*argv, "%lg", &probability) != 1) {
+				fprintf(stderr, "Illegal \"probability\"\n");
+				return -1;
+			}
+			red++;
+		} else if (strcmp(*argv, "ecn") == 0) {
+			opt.flags |= TC_RED_ECN;
+			red++;
+		} else if (strcmp(*argv, "harddrop") == 0) {
+			opt.flags |= TC_RED_HARDDROP;
+			red++;
 		} else if (strcmp(*argv, "help") == 0) {
 			explain();
 			return -1;
@@ -79,8 +154,51 @@
 		}
 		argc--; argv++;
 	}
+	if (red) {
+		if (!opt.limit) {
+			fprintf(stderr, "Required parameter (redflowlimit) is missing\n");
+			return -1;
+		}
+		/* Compute default min/max thresholds based on 
+		   Sally Floyd's recommendations:
+		   http://www.icir.org/floyd/REDparameters.txt
+		*/
+		if (!opt.qth_max) 
+			opt.qth_max = opt.limit / 4;
+		if (!opt.qth_min)
+			opt.qth_min = opt.qth_max / 3;
+		if (!burst)
+			burst = (2 * opt.qth_min + opt.qth_max) / (3 * avpkt);
 
-	if (ok)
+		if (opt.qth_max > opt.limit) {
+			fprintf(stderr, "\"max\" is larger than \"limit\"\n");
+			return -1;
+		}
+
+		if (opt.qth_min >= opt.qth_max) {
+			fprintf(stderr, "\"min\" is not smaller than \"max\"\n");
+			return -1;
+		}
+
+		wlog = tc_red_eval_ewma(opt.qth_min, burst, avpkt);
+		if (wlog < 0) {
+			fprintf(stderr, "SFQ: failed to calculate EWMA constant.\n");
+			return -1;
+		}
+		if (wlog >= 10)
+			fprintf(stderr, "SFQ: WARNING. Burst %u seems to be too large.\n", burst);
+		opt.Wlog = wlog;
+
+		wlog = tc_red_eval_P(opt.qth_min, opt.qth_max, probability);
+		if (wlog < 0) {
+			fprintf(stderr, "SFQ: failed to calculate probability.\n");
+			return -1;
+		}
+		opt.Plog = wlog;
+		opt.max_P = probability * pow(2, 32);
+	}
+
+	if (ok || red)
 		addattr_l(n, 1024, TCA_OPTIONS, &opt, sizeof(opt));
 	return 0;
 }
@@ -88,22 +206,50 @@
 static int sfq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
 {
 	struct tc_sfq_qopt *qopt;
+	struct tc_sfq_qopt_v1 *qopt_ext = NULL;
 	SPRINT_BUF(b1);
-
+	SPRINT_BUF(b2);
+	SPRINT_BUF(b3);
 	if (opt == NULL)
 		return 0;
 
 	if (RTA_PAYLOAD(opt)  < sizeof(*qopt))
 		return -1;
+	if (RTA_PAYLOAD(opt) >= sizeof(*qopt_ext))
+		qopt_ext = RTA_DATA(opt);
 	qopt = RTA_DATA(opt);
 	fprintf(f, "limit %up ", qopt->limit);
 	fprintf(f, "quantum %s ", sprint_size(qopt->quantum, b1));
+	if (qopt_ext && qopt_ext->depth)
+		fprintf(f, "depth %u ", qopt_ext->depth);
+	if (qopt_ext && qopt_ext->headdrop)
+		fprintf(f, "headdrop ");
+
 	if (show_details) {
 		fprintf(f, "flows %u/%u ", qopt->flows, qopt->divisor);
 	}
 	fprintf(f, "divisor %u ", qopt->divisor);
 	if (qopt->perturb_period)
 		fprintf(f, "perturb %dsec ", qopt->perturb_period);
+	if (qopt_ext && qopt_ext->qth_min) {
+		fprintf(f, "\n ewma %u ", qopt_ext->Wlog);
+		fprintf(f, "min %s max %s probability %g ",
+			sprint_size(qopt_ext->qth_min, b2),
+			sprint_size(qopt_ext->qth_max, b3),
+			qopt_ext->max_P / pow(2, 32));
+		if (qopt_ext->flags & TC_RED_ECN)
+			fprintf(f, "ecn ");
+		if (show_stats) {
+			fprintf(f, "\n prob_mark %u prob_mark_head %u prob_drop %u",
+				qopt_ext->stats.prob_mark,
+				qopt_ext->stats.prob_mark_head,
+				qopt_ext->stats.prob_drop);
+			fprintf(f, "\n forced_mark %u forced_mark_head %u forced_drop %u",
+				qopt_ext->stats.forced_mark,
+				qopt_ext->stats.forced_mark_head,
+				qopt_ext->stats.forced_drop);
+		}
+	}
 	return 0;
 }