#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
-#include <syslog.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
+#include <math.h>
#include "utils.h"
#include "tc_util.h"
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");
}
-static int red_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
+static int red_parse_opt(struct qdisc_util *qu, int argc, char **argv,
+ struct nlmsghdr *n, const char *dev)
{
- struct tc_red_qopt opt;
- unsigned burst = 0;
- unsigned avpkt = 0;
+ struct tc_red_qopt opt = {};
+ unsigned int burst = 0;
+ unsigned int avpkt = 0;
double probability = 0.02;
- unsigned rate = 0;
- int wlog;
+ unsigned int rate = 0;
+ int parm;
__u8 sbuf[256];
__u32 max_P;
struct rtattr *tail;
- memset(&opt, 0, sizeof(opt));
-
while (argc > 0) {
if (strcmp(*argv, "limit") == 0) {
NEXT_ARG();
}
} else if (strcmp(*argv, "bandwidth") == 0) {
NEXT_ARG();
- if (get_rate(&rate, *argv)) {
+ if (strchr(*argv, '%')) {
+ if (get_percent_rate(&rate, *argv, dev)) {
+ fprintf(stderr, "Illegal \"bandwidth\"\n");
+ return -1;
+ }
+ } else if (get_rate(&rate, *argv)) {
fprintf(stderr, "Illegal \"bandwidth\"\n");
return -1;
}
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;
argc--; argv++;
}
- if (rate == 0)
- get_rate(&rate, "10Mbit");
-
if (!opt.limit || !avpkt) {
fprintf(stderr, "RED: Required parameter (limit, avpkt) is missing\n");
return -1;
opt.qth_min = opt.qth_max / 3;
if (!burst)
burst = (2 * opt.qth_min + opt.qth_max) / (3 * avpkt);
- if ((wlog = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) {
+ if (!rate) {
+ get_rate(&rate, "10Mbit");
+ fprintf(stderr, "RED: set bandwidth to 10Mbit\n");
+ }
+ if ((parm = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) {
fprintf(stderr, "RED: failed to calculate EWMA constant.\n");
return -1;
}
- if (wlog >= 10)
- fprintf(stderr, "RED: WARNING. Burst %d seems to be too large.\n", burst);
- opt.Wlog = wlog;
- if ((wlog = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) {
+ if (parm >= 10)
+ fprintf(stderr, "RED: WARNING. Burst %u seems to be too large.\n", burst);
+ opt.Wlog = parm;
+ if ((parm = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) {
fprintf(stderr, "RED: failed to calculate probability.\n");
return -1;
}
- opt.Plog = wlog;
- if ((wlog = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0) {
+ opt.Plog = parm;
+ if ((parm = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0) {
fprintf(stderr, "RED: failed to calculate idle damping table.\n");
return -1;
}
- opt.Scell_log = wlog;
+ opt.Scell_log = parm;
- tail = NLMSG_TAIL(n);
- addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
+ tail = addattr_nest(n, 1024, TCA_OPTIONS);
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;
+ addattr_nest_end(n, tail);
return 0;
}
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);
if (tb[TCA_RED_MAX_P] &&
RTA_PAYLOAD(tb[TCA_RED_MAX_P]) >= sizeof(__u32))
- max_P = *(__u32 *)RTA_DATA(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));
- if (qopt->flags & TC_RED_ECN)
- fprintf(f, "ecn ");
- if (qopt->flags & TC_RED_HARDDROP)
- fprintf(f, "harddrop ");
- if (qopt->flags & TC_RED_ADAPTATIVE)
- fprintf(f, "adaptative ");
+ max_P = rta_getattr_u32(tb[TCA_RED_MAX_P]);
+
+ print_uint(PRINT_JSON, "limit", NULL, qopt->limit);
+ print_string(PRINT_FP, NULL, "limit %s ", sprint_size(qopt->limit, b1));
+ print_uint(PRINT_JSON, "min", NULL, qopt->qth_min);
+ print_string(PRINT_FP, NULL, "min %s ", sprint_size(qopt->qth_min, b2));
+ print_uint(PRINT_JSON, "max", NULL, qopt->qth_max);
+ print_string(PRINT_FP, NULL, "max %s ", sprint_size(qopt->qth_max, b3));
+
+ tc_red_print_flags(qopt->flags);
+
if (show_details) {
- fprintf(f, "ewma %u ", qopt->Wlog);
+ print_uint(PRINT_ANY, "ewma", "ewma %u ", qopt->Wlog);
if (max_P)
- fprintf(f, "probability %lg ", max_P / pow(2, 32));
+ print_float(PRINT_ANY, "probability",
+ "probability %lg ", max_P / pow(2, 32));
else
- fprintf(f, "Plog %u ", qopt->Plog);
- fprintf(f, "Scell_log %u", qopt->Scell_log);
+ print_uint(PRINT_ANY, "Plog", "Plog %u ", qopt->Plog);
+ print_uint(PRINT_ANY, "Scell_log", "Scell_log %u",
+ qopt->Scell_log);
}
return 0;
}
return -1;
st = RTA_DATA(xstats);
- fprintf(f, " marked %u early %u pdrop %u other %u",
- st->marked, st->early, st->pdrop, st->other);
- return 0;
-
+ print_uint(PRINT_ANY, "marked", " marked %u ", st->marked);
+ print_uint(PRINT_ANY, "early", "early %u ", st->early);
+ print_uint(PRINT_ANY, "pdrop", "pdrop %u ", st->pdrop);
+ print_uint(PRINT_ANY, "other", "other %u ", st->other);
#endif
return 0;
}