Home | History | Annotate | Download | only in tc
      1 /*
      2  * q_gred.c		GRED.
      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:    J Hadi Salim(hadi (at) nortelnetworks.com)
     10  *             code ruthlessly ripped from
     11  *	       Alexey Kuznetsov, <kuznet (at) ms2.inr.ac.ru>
     12  *
     13  */
     14 
     15 #include <stdio.h>
     16 #include <stdlib.h>
     17 #include <unistd.h>
     18 #include <syslog.h>
     19 #include <fcntl.h>
     20 #include <sys/socket.h>
     21 #include <netinet/in.h>
     22 #include <arpa/inet.h>
     23 #include <string.h>
     24 
     25 #include "utils.h"
     26 #include "tc_util.h"
     27 
     28 #include "tc_red.h"
     29 
     30 
     31 #if 0
     32 #define DPRINTF(format,args...) fprintf(stderr,format,##args)
     33 #else
     34 #define DPRINTF(format,args...)
     35 #endif
     36 
     37 static void explain(void)
     38 {
     39 	fprintf(stderr, "Usage: ... gred DP drop-probability limit BYTES "
     40 	    "min BYTES max BYTES\n");
     41 	fprintf(stderr, "    avpkt BYTES burst PACKETS probability PROBABILITY "
     42 	    "bandwidth KBPS\n");
     43 	fprintf(stderr, "    [prio value]\n");
     44 	fprintf(stderr," OR ...\n");
     45 	fprintf(stderr," gred setup DPs <num of DPs> default <default DP> "
     46 	    "[grio]\n");
     47 }
     48 
     49 #define usage() return(-1)
     50 
     51 static int init_gred(struct qdisc_util *qu, int argc, char **argv,
     52 		     struct nlmsghdr *n)
     53 {
     54 
     55 	struct rtattr *tail;
     56 	struct tc_gred_sopt opt;
     57 	int dps = 0;
     58 	int def_dp = -1;
     59 
     60 	while (argc > 0) {
     61 		DPRINTF(stderr,"init_gred: invoked with %s\n",*argv);
     62 		if (strcmp(*argv, "DPs") == 0) {
     63 			NEXT_ARG();
     64 			DPRINTF(stderr,"init_gred: next_arg with %s\n",*argv);
     65 			dps = strtol(*argv, (char **)NULL, 10);
     66 			if (dps < 0 || dps >MAX_DPs) {
     67 				fprintf(stderr, "DPs =%d\n", dps);
     68 				fprintf(stderr, "Illegal \"DPs\"\n");
     69 				fprintf(stderr, "GRED: only %d DPs are "
     70 					"currently supported\n",MAX_DPs);
     71 				return -1;
     72 			}
     73 		} else if (strcmp(*argv, "default") == 0) {
     74 			NEXT_ARG();
     75 			def_dp = strtol(*argv, (char **)NULL, 10);
     76 			if (dps == 0) {
     77 				fprintf(stderr, "\"default DP\" must be "
     78 					"defined after DPs\n");
     79 				return -1;
     80 			}
     81 			if (def_dp < 0 || def_dp > dps) {
     82 				fprintf(stderr,
     83 					"\"default DP\" must be less than %d\n",
     84 					opt.DPs);
     85 				return -1;
     86 			}
     87 		} else if (strcmp(*argv, "grio") == 0) {
     88 			opt.grio=1;
     89 		} else if (strcmp(*argv, "help") == 0) {
     90 			explain();
     91 			return -1;
     92 		} else {
     93 			fprintf(stderr, "What is \"%s\"?\n", *argv);
     94 			explain();
     95 			return -1;
     96 		}
     97 		argc--; argv++;
     98 	}
     99 
    100 	if (!dps || def_dp == -1) {
    101 		fprintf(stderr, "Illegal gred setup parameters \n");
    102 		return -1;
    103 	}
    104 
    105 	memset(&opt, 0, sizeof(struct tc_gred_sopt));
    106 	opt.DPs = dps;
    107 	opt.def_DP = def_dp;
    108 
    109 	DPRINTF("TC_GRED: sending DPs=%d default=%d\n",opt.DPs,opt.def_DP);
    110 	n->nlmsg_flags|=NLM_F_CREATE;
    111 	tail = NLMSG_TAIL(n);
    112 	addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
    113 	addattr_l(n, 1024, TCA_GRED_DPS, &opt, sizeof(struct tc_gred_sopt));
    114 	tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
    115 	return 0;
    116 }
    117 /*
    118 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    119 */
    120 static int gred_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
    121 {
    122 	int ok=0;
    123 	struct tc_gred_qopt opt;
    124 	unsigned burst = 0;
    125 	unsigned avpkt = 0;
    126 	double probability = 0.02;
    127 	unsigned rate = 0;
    128 	int wlog;
    129 	__u8 sbuf[256];
    130 	struct rtattr *tail;
    131 
    132 	memset(&opt, 0, sizeof(opt));
    133 
    134 	while (argc > 0) {
    135 		if (strcmp(*argv, "limit") == 0) {
    136 			NEXT_ARG();
    137 			if (get_size(&opt.limit, *argv)) {
    138 				fprintf(stderr, "Illegal \"limit\"\n");
    139 				return -1;
    140 			}
    141 			ok++;
    142 		} else if (strcmp(*argv, "setup") == 0) {
    143 			if (ok) {
    144 				fprintf(stderr, "Illegal \"setup\"\n");
    145 				return -1;
    146 			}
    147 		return init_gred(qu,argc-1, argv+1,n);
    148 
    149 		} else if (strcmp(*argv, "min") == 0) {
    150 			NEXT_ARG();
    151 			if (get_size(&opt.qth_min, *argv)) {
    152 				fprintf(stderr, "Illegal \"min\"\n");
    153 				return -1;
    154 			}
    155 			ok++;
    156 		} else if (strcmp(*argv, "max") == 0) {
    157 			NEXT_ARG();
    158 			if (get_size(&opt.qth_max, *argv)) {
    159 				fprintf(stderr, "Illegal \"max\"\n");
    160 				return -1;
    161 			}
    162 			ok++;
    163 		} else if (strcmp(*argv, "DP") == 0) {
    164 			NEXT_ARG();
    165 			opt.DP=strtol(*argv, (char **)NULL, 10);
    166 			DPRINTF ("\n ******* DP =%u\n",opt.DP);
    167 			if (opt.DP >MAX_DPs) { /* need a better error check */
    168 				fprintf(stderr, "DP =%u \n",opt.DP);
    169 				fprintf(stderr, "Illegal \"DP\"\n");
    170 				fprintf(stderr, "GRED: only %d DPs are currently supported\n",MAX_DPs);
    171 				return -1;
    172 			}
    173 			ok++;
    174 		} else if (strcmp(*argv, "burst") == 0) {
    175 			NEXT_ARG();
    176                         if (get_unsigned(&burst, *argv, 0)) {
    177 				fprintf(stderr, "Illegal \"burst\"\n");
    178 				return -1;
    179 			}
    180 			ok++;
    181 		} else if (strcmp(*argv, "avpkt") == 0) {
    182 			NEXT_ARG();
    183 			if (get_size(&avpkt, *argv)) {
    184 				fprintf(stderr, "Illegal \"avpkt\"\n");
    185 				return -1;
    186 			}
    187 			ok++;
    188 		} else if (strcmp(*argv, "probability") == 0) {
    189 			NEXT_ARG();
    190 			if (sscanf(*argv, "%lg", &probability) != 1) {
    191 				fprintf(stderr, "Illegal \"probability\"\n");
    192 				return -1;
    193 			}
    194 			ok++;
    195 		} else if (strcmp(*argv, "prio") == 0) {
    196 			NEXT_ARG();
    197 			opt.prio=strtol(*argv, (char **)NULL, 10);
    198 			/* some error check here */
    199 			ok++;
    200 		} else if (strcmp(*argv, "bandwidth") == 0) {
    201 			NEXT_ARG();
    202 			if (get_rate(&rate, *argv)) {
    203 				fprintf(stderr, "Illegal \"bandwidth\"\n");
    204 				return -1;
    205 			}
    206 			ok++;
    207 		} else if (strcmp(*argv, "help") == 0) {
    208 			explain();
    209 			return -1;
    210 		} else {
    211 			fprintf(stderr, "What is \"%s\"?\n", *argv);
    212 			explain();
    213 			return -1;
    214 		}
    215 		argc--; argv++;
    216 	}
    217 
    218 	if (!ok)
    219 		return 0;
    220 
    221 	if (rate == 0)
    222 		get_rate(&rate, "10Mbit");
    223 
    224 	if (!opt.qth_min || !opt.qth_max || !burst || !opt.limit || !avpkt ||
    225 	    (opt.DP<0)) {
    226 		fprintf(stderr, "Required parameter (min, max, burst, limit, "
    227 		    "avpket, DP) is missing\n");
    228 		return -1;
    229 	}
    230 
    231 	if ((wlog = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) {
    232 		fprintf(stderr, "GRED: failed to calculate EWMA constant.\n");
    233 		return -1;
    234 	}
    235 	if (wlog >= 10)
    236 		fprintf(stderr, "GRED: WARNING. Burst %d seems to be to "
    237 		    "large.\n", burst);
    238 	opt.Wlog = wlog;
    239 	if ((wlog = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) {
    240 		fprintf(stderr, "GRED: failed to calculate probability.\n");
    241 		return -1;
    242 	}
    243 	opt.Plog = wlog;
    244 	if ((wlog = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0)
    245 	    {
    246 		fprintf(stderr, "GRED: failed to calculate idle damping "
    247 		    "table.\n");
    248 		return -1;
    249 	}
    250 	opt.Scell_log = wlog;
    251 
    252 	tail = NLMSG_TAIL(n);
    253 	addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
    254 	addattr_l(n, 1024, TCA_GRED_PARMS, &opt, sizeof(opt));
    255 	addattr_l(n, 1024, TCA_GRED_STAB, sbuf, 256);
    256 	tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
    257 	return 0;
    258 }
    259 
    260 static int gred_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
    261 {
    262 	struct rtattr *tb[TCA_GRED_STAB+1];
    263 	struct tc_gred_qopt *qopt;
    264 	int i;
    265 	SPRINT_BUF(b1);
    266 	SPRINT_BUF(b2);
    267 	SPRINT_BUF(b3);
    268 	SPRINT_BUF(b4);
    269 	SPRINT_BUF(b5);
    270 
    271 	if (opt == NULL)
    272 		return 0;
    273 
    274 	parse_rtattr_nested(tb, TCA_GRED_STAB, opt);
    275 
    276 	if (tb[TCA_GRED_PARMS] == NULL)
    277 		return -1;
    278 
    279 	qopt = RTA_DATA(tb[TCA_GRED_PARMS]);
    280 	if (RTA_PAYLOAD(tb[TCA_GRED_PARMS])  < sizeof(*qopt)*MAX_DPs) {
    281 		fprintf(f,"\n GRED received message smaller than expected\n");
    282 		return -1;
    283 		}
    284 
    285 /* Bad hack! should really return a proper message as shown above*/
    286 
    287 	for (i=0;i<MAX_DPs;i++, qopt++) {
    288 		if (qopt->DP >= MAX_DPs) continue;
    289 		fprintf(f, "\n DP:%d (prio %d) Average Queue %s Measured "
    290 		    "Queue %s  ",
    291 			qopt->DP,
    292 			qopt->prio,
    293 			sprint_size(qopt->qave, b4),
    294 			sprint_size(qopt->backlog, b5));
    295 		fprintf(f, "\n\t Packet drops: %d (forced %d early %d)  ",
    296 			qopt->forced+qopt->early,
    297 			qopt->forced,
    298 			qopt->early);
    299 		fprintf(f, "\n\t Packet totals: %u (bytes %u)  ",
    300 			qopt->packets,
    301 			qopt->bytesin);
    302 		if (show_details)
    303 			fprintf(f, "\n limit %s min %s max %s ",
    304 				sprint_size(qopt->limit, b1),
    305 				sprint_size(qopt->qth_min, b2),
    306 				sprint_size(qopt->qth_max, b3));
    307 				fprintf(f, "ewma %u Plog %u Scell_log %u",
    308 				    qopt->Wlog, qopt->Plog, qopt->Scell_log);
    309 	}
    310 	return 0;
    311 }
    312 
    313 struct qdisc_util gred_qdisc_util = {
    314 	.id		= "gred",
    315 	.parse_qopt	= gred_parse_opt,
    316 	.print_qopt	= gred_print_opt,
    317 };
    318