Home | History | Annotate | Download | only in tc
      1 /*
      2  * q_rr.c		RR.
      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:	PJ Waskiewicz, <peter.p.waskiewicz.jr (at) intel.com>
     10  * Original Authors:	Alexey Kuznetsov, <kuznet (at) ms2.inr.ac.ru> (from PRIO)
     11  */
     12 
     13 #include <stdio.h>
     14 #include <stdlib.h>
     15 #include <unistd.h>
     16 #include <syslog.h>
     17 #include <fcntl.h>
     18 #include <sys/socket.h>
     19 #include <netinet/in.h>
     20 #include <arpa/inet.h>
     21 #include <string.h>
     22 
     23 #include "utils.h"
     24 #include "tc_util.h"
     25 
     26 static void explain(void)
     27 {
     28 	fprintf(stderr, "Usage: ... rr bands NUMBER priomap P1 P2... [multiqueue]\n");
     29 }
     30 
     31 #define usage() return(-1)
     32 
     33 static int rr_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
     34 {
     35 	int ok = 0;
     36 	int pmap_mode = 0;
     37 	int idx = 0;
     38 	struct tc_prio_qopt opt={3,{ 1, 2, 2, 2, 1, 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 }};
     39 	struct rtattr *nest;
     40 	unsigned char mq = 0;
     41 
     42 	while (argc > 0) {
     43 		if (strcmp(*argv, "bands") == 0) {
     44 			if (pmap_mode)
     45 				explain();
     46 			NEXT_ARG();
     47 			if (get_integer(&opt.bands, *argv, 10)) {
     48 				fprintf(stderr, "Illegal \"bands\"\n");
     49 				return -1;
     50 			}
     51 			ok++;
     52 		} else if (strcmp(*argv, "priomap") == 0) {
     53 			if (pmap_mode) {
     54 				fprintf(stderr, "Error: duplicate priomap\n");
     55 				return -1;
     56 			}
     57 			pmap_mode = 1;
     58 		} else if (strcmp(*argv, "help") == 0) {
     59 			explain();
     60 			return -1;
     61 		} else if (strcmp(*argv, "multiqueue") == 0) {
     62 			mq = 1;
     63 		} else {
     64 			unsigned band;
     65 			if (!pmap_mode) {
     66 				fprintf(stderr, "What is \"%s\"?\n", *argv);
     67 				explain();
     68 				return -1;
     69 			}
     70 			if (get_unsigned(&band, *argv, 10)) {
     71 				fprintf(stderr, "Illegal \"priomap\" element\n");
     72 				return -1;
     73 			}
     74 			if (band > opt.bands) {
     75 				fprintf(stderr, "\"priomap\" element is out of bands\n");
     76 				return -1;
     77 			}
     78 			if (idx > TC_PRIO_MAX) {
     79 				fprintf(stderr, "\"priomap\" index > TC_RR_MAX=%u\n", TC_PRIO_MAX);
     80 				return -1;
     81 			}
     82 			opt.priomap[idx++] = band;
     83 		}
     84 		argc--; argv++;
     85 	}
     86 
     87 	nest = addattr_nest_compat(n, 1024, TCA_OPTIONS, &opt, sizeof(opt));
     88 	if (mq)
     89 		addattr_l(n, 1024, TCA_PRIO_MQ, NULL, 0);
     90 	addattr_nest_compat_end(n, nest);
     91 	return 0;
     92 }
     93 
     94 int rr_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
     95 {
     96 	int i;
     97 	struct tc_prio_qopt *qopt;
     98 	struct rtattr *tb[TCA_PRIO_MAX + 1];
     99 
    100 	if (opt == NULL)
    101 		return 0;
    102 
    103 	if (parse_rtattr_nested_compat(tb, TCA_PRIO_MAX, opt, qopt,
    104 						sizeof(*qopt)))
    105 		return -1;
    106 
    107 	fprintf(f, "bands %u priomap ", qopt->bands);
    108 	for (i=0; i <= TC_PRIO_MAX; i++)
    109 		fprintf(f, " %d", qopt->priomap[i]);
    110 
    111 	if (tb[TCA_PRIO_MQ])
    112 		fprintf(f, " multiqueue: %s ",
    113 		    *(unsigned char *)RTA_DATA(tb[TCA_PRIO_MQ]) ? "on" : "off");
    114 
    115 	return 0;
    116 }
    117 
    118 struct qdisc_util rr_qdisc_util = {
    119 	.id	 	= "rr",
    120 	.parse_qopt	= rr_parse_opt,
    121 	.print_qopt	= rr_print_opt,
    122 };
    123