]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/blobdiff - tc/q_red.c
Convert to use rta_getattr_ functions
[lisovros/iproute2_canprio.git] / tc / q_red.c
index 6e58d7a788e32845652357b4cf89d5d9d1ae4be6..89e7320181d52ae0c966580c1192f88046842339 100644 (file)
@@ -19,6 +19,7 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <string.h>
+#include <math.h>
 
 #include "utils.h"
 #include "tc_util.h"
@@ -27,8 +28,9 @@
 
 static void explain(void)
 {
-       fprintf(stderr, "Usage: ... red limit BYTES min BYTES max BYTES avpkt BYTES burst PACKETS\n");
-       fprintf(stderr, "               probability PROBABILITY bandwidth KBPS [ ecn ]\n");
+       fprintf(stderr, "Usage: ... red limit BYTES [min BYTES] [max BYTES] avpkt BYTES [burst PACKETS]\n");
+       fprintf(stderr, "               [adaptive] [probability PROBABILITY] bandwidth KBPS\n");
+       fprintf(stderr, "               [ecn] [harddrop]\n");
 }
 
 static int red_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
@@ -38,9 +40,9 @@ static int red_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nl
        unsigned avpkt = 0;
        double probability = 0.02;
        unsigned rate = 0;
-       int ecn_ok = 0;
        int wlog;
        __u8 sbuf[256];
+       __u32 max_P;
        struct rtattr *tail;
 
        memset(&opt, 0, sizeof(opt));
@@ -89,7 +91,13 @@ static int red_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nl
                                return -1;
                        }
                } else if (strcmp(*argv, "ecn") == 0) {
-                       ecn_ok = 1;
+                       opt.flags |= TC_RED_ECN;
+               } else if (strcmp(*argv, "harddrop") == 0) {
+                       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;
@@ -104,14 +112,20 @@ static int red_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nl
        if (rate == 0)
                get_rate(&rate, "10Mbit");
 
-       if (!opt.qth_min || !opt.qth_max || !opt.limit || !avpkt) {
-               fprintf(stderr, "RED: Required parameter (min, max, limit, avpkt) is missing\n");
+       if (!opt.limit || !avpkt) {
+               fprintf(stderr, "RED: Required parameter (limit, avpkt) is missing\n");
                return -1;
        }
-       if (!burst) {
+       /* 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.qth_min ? opt.qth_min * 3 : 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);
-               fprintf(stderr, "RED: set burst to %u\n", burst);
-       }
        if ((wlog = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) {
                fprintf(stderr, "RED: failed to calculate EWMA constant.\n");
                return -1;
@@ -129,27 +143,22 @@ static int red_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nl
                return -1;
        }
        opt.Scell_log = wlog;
-       if (ecn_ok) {
-#ifdef TC_RED_ECN
-               opt.flags |= TC_RED_ECN;
-#else
-               fprintf(stderr, "RED: ECN support is missing in this binary.\n");
-               return -1;
-#endif
-       }
 
        tail = NLMSG_TAIL(n);
        addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
        addattr_l(n, 1024, TCA_RED_PARMS, &opt, sizeof(opt));
        addattr_l(n, 1024, TCA_RED_STAB, sbuf, 256);
+       max_P = probability * pow(2, 32);
+       addattr_l(n, 1024, TCA_RED_MAX_P, &max_P, sizeof(max_P));
        tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
        return 0;
 }
 
 static int red_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
 {
-       struct rtattr *tb[TCA_RED_STAB+1];
+       struct rtattr *tb[TCA_RED_MAX + 1];
        struct tc_red_qopt *qopt;
+       __u32 max_P = 0;
        SPRINT_BUF(b1);
        SPRINT_BUF(b2);
        SPRINT_BUF(b3);
@@ -157,24 +166,35 @@ static int red_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
        if (opt == NULL)
                return 0;
 
-       parse_rtattr_nested(tb, TCA_RED_STAB, opt);
+       parse_rtattr_nested(tb, TCA_RED_MAX, opt);
 
        if (tb[TCA_RED_PARMS] == NULL)
                return -1;
        qopt = RTA_DATA(tb[TCA_RED_PARMS]);
        if (RTA_PAYLOAD(tb[TCA_RED_PARMS])  < sizeof(*qopt))
                return -1;
+
+       if (tb[TCA_RED_MAX_P] &&
+           RTA_PAYLOAD(tb[TCA_RED_MAX_P]) >= sizeof(__u32))
+               max_P = rta_getattr_u32(tb[TCA_RED_MAX_P]);
+
        fprintf(f, "limit %s min %s max %s ",
                sprint_size(qopt->limit, b1),
                sprint_size(qopt->qth_min, b2),
                sprint_size(qopt->qth_max, b3));
-#ifdef TC_RED_ECN
        if (qopt->flags & TC_RED_ECN)
                fprintf(f, "ecn ");
-#endif
+       if (qopt->flags & TC_RED_HARDDROP)
+               fprintf(f, "harddrop ");
+       if (qopt->flags & TC_RED_ADAPTATIVE)
+               fprintf(f, "adaptive ");
        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 %lg ", max_P / pow(2, 32));
+               else
+                       fprintf(f, "Plog %u ", qopt->Plog);
+               fprintf(f, "Scell_log %u", qopt->Scell_log);
        }
        return 0;
 }