Tagging module iproute2 - iproute2-2.6.16-2
[iproute2.git] / tc / q_prio.c
1 /*
2  * q_prio.c             PRIO.
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:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  *
11  * Changes:
12  *
13  * Ole Husgaard <sparre@login.dknet.dk>: 990513: prio2band map was always reset.
14  * J Hadi Salim <hadi@cyberus.ca>: 990609: priomap fix.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <syslog.h>
21 #include <fcntl.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <string.h>
26
27 #include "utils.h"
28 #include "tc_util.h"
29
30 static void explain(void)
31 {
32         fprintf(stderr, "Usage: ... prio bands NUMBER priomap P1 P2...\n");
33 }
34
35 #define usage() return(-1)
36
37 static int prio_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
38 {
39         int ok=0;
40         int pmap_mode = 0;
41         int idx = 0;
42         struct tc_prio_qopt opt={3,{ 1, 2, 2, 2, 1, 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 }};
43
44         while (argc > 0) {
45                 if (strcmp(*argv, "bands") == 0) {
46                         if (pmap_mode)
47                                 explain();
48                         NEXT_ARG();
49                         if (get_integer(&opt.bands, *argv, 10)) {
50                                 fprintf(stderr, "Illegal \"bands\"\n");
51                                 return -1;
52                         }
53                         ok++;
54                 } else if (strcmp(*argv, "priomap") == 0) {
55                         if (pmap_mode) {
56                                 fprintf(stderr, "Error: duplicate priomap\n");
57                                 return -1;
58                         }
59                         pmap_mode = 1;
60                 } else if (strcmp(*argv, "help") == 0) {
61                         explain();
62                         return -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_PRIO_MAX=%u\n", TC_PRIO_MAX);
80                                 return -1;
81                         }
82                         opt.priomap[idx++] = band;
83                 }
84                 argc--; argv++;
85         }
86
87 /*
88         if (pmap_mode) {
89                 for (; idx < TC_PRIO_MAX; idx++)
90                         opt.priomap[idx] = opt.priomap[TC_PRIO_BESTEFFORT];
91         }
92 */
93         addattr_l(n, 1024, TCA_OPTIONS, &opt, sizeof(opt));
94         return 0;
95 }
96
97 int prio_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
98 {
99         int i;
100         struct tc_prio_qopt *qopt;
101
102         if (opt == NULL)
103                 return 0;
104
105         if (RTA_PAYLOAD(opt)  < sizeof(*qopt))
106                 return -1;
107         qopt = RTA_DATA(opt);
108         fprintf(f, "bands %u priomap ", qopt->bands);
109         for (i=0; i<=TC_PRIO_MAX; i++)
110                 fprintf(f, " %d", qopt->priomap[i]);
111         return 0;
112 }
113
114 struct qdisc_util prio_qdisc_util = {
115         .id             = "prio",
116         .parse_qopt     = prio_parse_opt,
117         .print_qopt     = prio_print_opt,
118 };
119