Home | History | Annotate | Download | only in tc
      1 /*
      2  * q_choke.c		CHOKE.
      3  *
      4  *		This program is free software; you can redistribute it and/or
      5  *		modify it under the terms of the GNU General Public License
      6  *		as published by the Free Software Foundation; either version
      7  *		2 of the License, or (at your option) any later version.
      8  *
      9  * Authors:	Stephen Hemminger <shemminger (at) vyatta.com>
     10  */
     11 
     12 #include <stdio.h>
     13 #include <stdlib.h>
     14 #include <unistd.h>
     15 #include <syslog.h>
     16 #include <fcntl.h>
     17 #include <sys/socket.h>
     18 #include <netinet/in.h>
     19 #include <arpa/inet.h>
     20 #include <string.h>
     21 #include <math.h>
     22 
     23 #include "utils.h"
     24 #include "tc_util.h"
     25 
     26 #include "tc_red.h"
     27 
     28 static void explain(void)
     29 {
     30 	fprintf(stderr, "Usage: ... choke limit PACKETS bandwidth KBPS [ecn]\n");
     31 	fprintf(stderr, "                 [ min PACKETS ] [ max PACKETS ] [ burst PACKETS ]\n");
     32 }
     33 
     34 static int choke_parse_opt(struct qdisc_util *qu, int argc, char **argv,
     35 			   struct nlmsghdr *n)
     36 {
     37 	struct tc_red_qopt opt;
     38 	unsigned burst = 0;
     39 	unsigned avpkt = 1000;
     40 	double probability = 0.02;
     41 	unsigned rate = 0;
     42 	int ecn_ok = 0;
     43 	int wlog;
     44 	__u8 sbuf[256];
     45 	__u32 max_P;
     46 	struct rtattr *tail;
     47 
     48 	memset(&opt, 0, sizeof(opt));
     49 
     50 	while (argc > 0) {
     51 		if (strcmp(*argv, "limit") == 0) {
     52 			NEXT_ARG();
     53 			if (get_unsigned(&opt.limit, *argv, 0)) {
     54 				fprintf(stderr, "Illegal \"limit\"\n");
     55 				return -1;
     56 			}
     57 		} else if (strcmp(*argv, "bandwidth") == 0) {
     58 			NEXT_ARG();
     59 			if (get_rate(&rate, *argv)) {
     60 				fprintf(stderr, "Illegal \"bandwidth\"\n");
     61 				return -1;
     62 			}
     63 		} else if (strcmp(*argv, "ecn") == 0) {
     64 			ecn_ok = 1;
     65 		} else if (strcmp(*argv, "min") == 0) {
     66 			NEXT_ARG();
     67 			if (get_unsigned(&opt.qth_min, *argv, 0)) {
     68 				fprintf(stderr, "Illegal \"min\"\n");
     69 				return -1;
     70 			}
     71 		} else if (strcmp(*argv, "max") == 0) {
     72 			NEXT_ARG();
     73 			if (get_unsigned(&opt.qth_max, *argv, 0)) {
     74 				fprintf(stderr, "Illegal \"max\"\n");
     75 				return -1;
     76 			}
     77 		} else if (strcmp(*argv, "burst") == 0) {
     78 			NEXT_ARG();
     79 			if (get_unsigned(&burst, *argv, 0)) {
     80 				fprintf(stderr, "Illegal \"burst\"\n");
     81 				return -1;
     82 			}
     83 		} else if (strcmp(*argv, "avpkt") == 0) {
     84 			NEXT_ARG();
     85 			if (get_size(&avpkt, *argv)) {
     86 				fprintf(stderr, "Illegal \"avpkt\"\n");
     87 				return -1;
     88 			}
     89 		} else if (strcmp(*argv, "probability") == 0) {
     90 			NEXT_ARG();
     91 			if (sscanf(*argv, "%lg", &probability) != 1) {
     92 				fprintf(stderr, "Illegal \"probability\"\n");
     93 				return -1;
     94 			}
     95 		} else if (strcmp(*argv, "help") == 0) {
     96 			explain();
     97 			return -1;
     98 		} else {
     99 			fprintf(stderr, "What is \"%s\"?\n", *argv);
    100 			explain();
    101 			return -1;
    102 		}
    103 		argc--; argv++;
    104 	}
    105 
    106 	if (!rate || !opt.limit) {
    107 		fprintf(stderr, "Required parameter (bandwidth, limit) is missing\n");
    108 		return -1;
    109 	}
    110 
    111 	/* Compute default min/max thresholds based on
    112 	   Sally Floyd's recommendations:
    113 	   http://www.icir.org/floyd/REDparameters.txt
    114 	*/
    115 	if (!opt.qth_max)
    116 		opt.qth_max = opt.limit / 4;
    117 	if (!opt.qth_min)
    118 		opt.qth_min = opt.qth_max / 3;
    119 	if (!burst)
    120 		burst = (2 * opt.qth_min + opt.qth_max) / 3;
    121 
    122 	if (opt.qth_max > opt.limit) {
    123 		fprintf(stderr, "\"max\" is larger than \"limit\"\n");
    124 		return -1;
    125 	}
    126 
    127 	if (opt.qth_min >= opt.qth_max) {
    128 		fprintf(stderr, "\"min\" is not smaller than \"max\"\n");
    129 		return -1;
    130 	}
    131 
    132 	wlog = tc_red_eval_ewma(opt.qth_min*avpkt, burst, avpkt);
    133 	if (wlog < 0) {
    134 		fprintf(stderr, "CHOKE: failed to calculate EWMA constant.\n");
    135 		return -1;
    136 	}
    137 	if (wlog >= 10)
    138 		fprintf(stderr, "CHOKE: WARNING. Burst %d seems to be too large.\n", burst);
    139 	opt.Wlog = wlog;
    140 
    141 	wlog = tc_red_eval_P(opt.qth_min*avpkt, opt.qth_max*avpkt, probability);
    142 	if (wlog < 0) {
    143 		fprintf(stderr, "CHOKE: failed to calculate probability.\n");
    144 		return -1;
    145 	}
    146 	opt.Plog = wlog;
    147 
    148 	wlog = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf);
    149 	if (wlog < 0) {
    150 		fprintf(stderr, "CHOKE: failed to calculate idle damping table.\n");
    151 		return -1;
    152 	}
    153 	opt.Scell_log = wlog;
    154 	if (ecn_ok)
    155 		opt.flags |= TC_RED_ECN;
    156 
    157 	tail = NLMSG_TAIL(n);
    158 	addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
    159 	addattr_l(n, 1024, TCA_CHOKE_PARMS, &opt, sizeof(opt));
    160 	addattr_l(n, 1024, TCA_CHOKE_STAB, sbuf, 256);
    161 	max_P = probability * pow(2, 32);
    162 	addattr_l(n, 1024, TCA_CHOKE_MAX_P, &max_P, sizeof(max_P));
    163 	tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
    164 	return 0;
    165 }
    166 
    167 static int choke_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
    168 {
    169 	struct rtattr *tb[TCA_CHOKE_MAX+1];
    170 	const struct tc_red_qopt *qopt;
    171 	__u32 max_P = 0;
    172 
    173 	if (opt == NULL)
    174 		return 0;
    175 
    176 	parse_rtattr_nested(tb, TCA_CHOKE_MAX, opt);
    177 
    178 	if (tb[TCA_CHOKE_PARMS] == NULL)
    179 		return -1;
    180 	qopt = RTA_DATA(tb[TCA_CHOKE_PARMS]);
    181 	if (RTA_PAYLOAD(tb[TCA_CHOKE_PARMS])  < sizeof(*qopt))
    182 		return -1;
    183 	if (tb[TCA_CHOKE_MAX_P] &&
    184 	    RTA_PAYLOAD(tb[TCA_CHOKE_MAX_P]) >= sizeof(__u32))
    185 		max_P = rta_getattr_u32(tb[TCA_CHOKE_MAX_P]);
    186 
    187 	fprintf(f, "limit %up min %up max %up ",
    188 		qopt->limit, qopt->qth_min, qopt->qth_max);
    189 
    190 	if (qopt->flags & TC_RED_ECN)
    191 		fprintf(f, "ecn ");
    192 
    193 	if (show_details) {
    194 		fprintf(f, "ewma %u ", qopt->Wlog);
    195 		if (max_P)
    196 			fprintf(f, "probability %g ", max_P / pow(2, 32));
    197 		else
    198 			fprintf(f, "Plog %u ", qopt->Plog);
    199 		fprintf(f, "Scell_log %u", qopt->Scell_log);
    200 	}
    201 	return 0;
    202 }
    203 
    204 static int choke_print_xstats(struct qdisc_util *qu, FILE *f,
    205 			      struct rtattr *xstats)
    206 {
    207 	struct tc_choke_xstats *st;
    208 
    209 	if (xstats == NULL)
    210 		return 0;
    211 
    212 	if (RTA_PAYLOAD(xstats) < sizeof(*st))
    213 		return -1;
    214 
    215 	st = RTA_DATA(xstats);
    216 	fprintf(f, "  marked %u early %u pdrop %u other %u matched %u",
    217 		st->marked, st->early, st->pdrop, st->other, st->matched);
    218 	return 0;
    219 
    220 }
    221 
    222 struct qdisc_util choke_qdisc_util = {
    223 	.id		= "choke",
    224 	.parse_qopt	= choke_parse_opt,
    225 	.print_qopt	= choke_print_opt,
    226 	.print_xstats	= choke_print_xstats,
    227 };
    228