]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/blob - tc/q_htb.c
Allow default DP of zero in gred
[lisovros/iproute2_canprio.git] / tc / q_htb.c
1 /*
2  * q_htb.c              HTB.
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:     Martin Devera, devik@cdi.cz
10  *
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 #define HTB_TC_VER 0x30003
27 #if HTB_TC_VER >> 16 != TC_HTB_PROTOVER
28 #error "Different kernel and TC HTB versions"
29 #endif
30
31 static void explain(void)
32 {
33         fprintf(stderr, "Usage: ... qdisc add ... htb [default N] [r2q N]\n"
34                 " default  minor id of class to which unclassified packets are sent {0}\n"
35                 " r2q      DRR quantums are computed as rate in Bps/r2q {10}\n"
36                 " debug    string of 16 numbers each 0-3 {0}\n\n"
37                 "... class add ... htb rate R1 [burst B1] [mpu B] [overhead O]\n"
38                 "                      [prio P] [slot S] [pslot PS]\n"
39                 "                      [ceil R2] [cburst B2] [mtu MTU] [quantum Q]\n"
40                 " rate     rate allocated to this class (class can still borrow)\n"
41                 " burst    max bytes burst which can be accumulated during idle period {computed}\n"
42                 " mpu      minimum packet size used in rate computations\n"
43                 " overhead per-packet size overhead used in rate computations\n"
44                 " linklay  adapting to a linklayer e.g. atm\n"
45                 " ceil     definite upper class rate (no borrows) {rate}\n"
46                 " cburst   burst but for ceil {computed}\n"
47                 " mtu      max packet size we create rate map for {1600}\n"
48                 " prio     priority of leaf; lower are served first {0}\n"
49                 " quantum  how much bytes to serve from leaf at once {use r2q}\n"
50                 "\nTC HTB version %d.%d\n",HTB_TC_VER>>16,HTB_TC_VER&0xffff
51                 );
52 }
53
54 static void explain1(char *arg)
55 {
56     fprintf(stderr, "Illegal \"%s\"\n", arg);
57     explain();
58 }
59
60
61 #define usage() return(-1)
62
63 static int htb_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
64 {
65         struct tc_htb_glob opt;
66         struct rtattr *tail;
67         unsigned i; char *p;
68         memset(&opt,0,sizeof(opt));
69         opt.rate2quantum = 10;
70         opt.version = 3;
71
72         while (argc > 0) {
73                 if (matches(*argv, "r2q") == 0) {
74                     NEXT_ARG();
75                     if (get_u32(&opt.rate2quantum, *argv, 10)) {
76                         explain1("r2q"); return -1;
77                     }
78                 } else if (matches(*argv, "default") == 0) {
79                     NEXT_ARG();
80                     if (get_u32(&opt.defcls, *argv, 16)) {
81                         explain1("default"); return -1;
82                     }
83                 } else if (matches(*argv, "debug") == 0) {
84                     NEXT_ARG(); p = *argv;
85                     for (i=0; i<16; i++,p++) {
86                         if (*p<'0' || *p>'3') break;
87                         opt.debug |= (*p-'0')<<(2*i);
88                     }
89                 } else {
90                         fprintf(stderr, "What is \"%s\"?\n", *argv);
91                         explain();
92                         return -1;
93                 }
94                 argc--; argv++;
95         }
96         tail = NLMSG_TAIL(n);
97         addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
98         addattr_l(n, 2024, TCA_HTB_INIT, &opt, NLMSG_ALIGN(sizeof(opt)));
99         tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
100         return 0;
101 }
102
103 static int htb_parse_class_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
104 {
105         int ok=0;
106         struct tc_htb_opt opt;
107         __u32 rtab[256],ctab[256];
108         unsigned buffer=0,cbuffer=0;
109         int cell_log=-1,ccell_log = -1;
110         unsigned mtu;
111         unsigned short mpu = 0;
112         unsigned short overhead = 0;
113         unsigned int linklayer  = LINKLAYER_ETHERNET; /* Assume ethernet */
114         struct rtattr *tail;
115
116         memset(&opt, 0, sizeof(opt)); mtu = 1600; /* eth packet len */
117
118         while (argc > 0) {
119                 if (matches(*argv, "prio") == 0) {
120                         NEXT_ARG();
121                         if (get_u32(&opt.prio, *argv, 10)) {
122                                 explain1("prio"); return -1;
123                         }
124                         ok++;
125                 } else if (matches(*argv, "mtu") == 0) {
126                         NEXT_ARG();
127                         if (get_u32(&mtu, *argv, 10)) {
128                                 explain1("mtu"); return -1;
129                         }
130                 } else if (matches(*argv, "mpu") == 0) {
131                         NEXT_ARG();
132                         if (get_u16(&mpu, *argv, 10)) {
133                                 explain1("mpu"); return -1;
134                         }
135                 } else if (matches(*argv, "overhead") == 0) {
136                         NEXT_ARG();
137                         if (get_u16(&overhead, *argv, 10)) {
138                                 explain1("overhead"); return -1;
139                         }
140                 } else if (matches(*argv, "linklayer") == 0) {
141                         NEXT_ARG();
142                         if (get_linklayer(&linklayer, *argv)) {
143                                 explain1("linklayer"); return -1;
144                         }
145                 } else if (matches(*argv, "quantum") == 0) {
146                         NEXT_ARG();
147                         if (get_u32(&opt.quantum, *argv, 10)) {
148                                 explain1("quantum"); return -1;
149                         }
150                 } else if (matches(*argv, "burst") == 0 ||
151                         strcmp(*argv, "buffer") == 0 ||
152                         strcmp(*argv, "maxburst") == 0) {
153                         NEXT_ARG();
154                         if (get_size_and_cell(&buffer, &cell_log, *argv) < 0) {
155                                 explain1("buffer");
156                                 return -1;
157                         }
158                         ok++;
159                 } else if (matches(*argv, "cburst") == 0 ||
160                         strcmp(*argv, "cbuffer") == 0 ||
161                         strcmp(*argv, "cmaxburst") == 0) {
162                         NEXT_ARG();
163                         if (get_size_and_cell(&cbuffer, &ccell_log, *argv) < 0) {
164                                 explain1("cbuffer");
165                                 return -1;
166                         }
167                         ok++;
168                 } else if (strcmp(*argv, "ceil") == 0) {
169                         NEXT_ARG();
170                         if (opt.ceil.rate) {
171                                 fprintf(stderr, "Double \"ceil\" spec\n");
172                                 return -1;
173                         }
174                         if (get_rate(&opt.ceil.rate, *argv)) {
175                                 explain1("ceil");
176                                 return -1;
177                         }
178                         ok++;
179                 } else if (strcmp(*argv, "rate") == 0) {
180                         NEXT_ARG();
181                         if (opt.rate.rate) {
182                                 fprintf(stderr, "Double \"rate\" spec\n");
183                                 return -1;
184                         }
185                         if (get_rate(&opt.rate.rate, *argv)) {
186                                 explain1("rate");
187                                 return -1;
188                         }
189                         ok++;
190                 } else if (strcmp(*argv, "help") == 0) {
191                         explain();
192                         return -1;
193                 } else {
194                         fprintf(stderr, "What is \"%s\"?\n", *argv);
195                         explain();
196                         return -1;
197                 }
198                 argc--; argv++;
199         }
200
201 /*      if (!ok)
202                 return 0;*/
203
204         if (opt.rate.rate == 0) {
205                 fprintf(stderr, "\"rate\" is required.\n");
206                 return -1;
207         }
208         /* if ceil params are missing, use the same as rate */
209         if (!opt.ceil.rate) opt.ceil = opt.rate;
210
211         /* compute minimal allowed burst from rate; mtu is added here to make
212            sute that buffer is larger than mtu and to have some safeguard space */
213         if (!buffer) buffer = opt.rate.rate / get_hz() + mtu;
214         if (!cbuffer) cbuffer = opt.ceil.rate / get_hz() + mtu;
215
216         opt.ceil.overhead = overhead;
217         opt.rate.overhead = overhead;
218
219         opt.ceil.mpu = mpu;
220         opt.rate.mpu = mpu;
221
222         if (tc_calc_rtable(&opt.rate, rtab, cell_log, mtu, linklayer) < 0) {
223                 fprintf(stderr, "htb: failed to calculate rate table.\n");
224                 return -1;
225         }
226         opt.buffer = tc_calc_xmittime(opt.rate.rate, buffer);
227
228         if (tc_calc_rtable(&opt.ceil, ctab, ccell_log, mtu, linklayer) < 0) {
229                 fprintf(stderr, "htb: failed to calculate ceil rate table.\n");
230                 return -1;
231         }
232         opt.cbuffer = tc_calc_xmittime(opt.ceil.rate, cbuffer);
233
234         tail = NLMSG_TAIL(n);
235         addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
236         addattr_l(n, 2024, TCA_HTB_PARMS, &opt, sizeof(opt));
237         addattr_l(n, 3024, TCA_HTB_RTAB, rtab, 1024);
238         addattr_l(n, 4024, TCA_HTB_CTAB, ctab, 1024);
239         tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
240         return 0;
241 }
242
243 static int htb_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
244 {
245         struct rtattr *tb[TCA_HTB_RTAB+1];
246         struct tc_htb_opt *hopt;
247         struct tc_htb_glob *gopt;
248         double buffer,cbuffer;
249         SPRINT_BUF(b1);
250         SPRINT_BUF(b2);
251         SPRINT_BUF(b3);
252
253         if (opt == NULL)
254                 return 0;
255
256         parse_rtattr_nested(tb, TCA_HTB_RTAB, opt);
257
258         if (tb[TCA_HTB_PARMS]) {
259
260             hopt = RTA_DATA(tb[TCA_HTB_PARMS]);
261             if (RTA_PAYLOAD(tb[TCA_HTB_PARMS])  < sizeof(*hopt)) return -1;
262
263                 if (!hopt->level) {
264                         fprintf(f, "prio %d ", (int)hopt->prio);
265                         if (show_details)
266                                 fprintf(f, "quantum %d ", (int)hopt->quantum);
267                 }
268             fprintf(f, "rate %s ", sprint_rate(hopt->rate.rate, b1));
269             buffer = tc_calc_xmitsize(hopt->rate.rate, hopt->buffer);
270             fprintf(f, "ceil %s ", sprint_rate(hopt->ceil.rate, b1));
271             cbuffer = tc_calc_xmitsize(hopt->ceil.rate, hopt->cbuffer);
272             if (show_details) {
273                 fprintf(f, "burst %s/%u mpu %s overhead %s ",
274                         sprint_size(buffer, b1),
275                         1<<hopt->rate.cell_log,
276                         sprint_size(hopt->rate.mpu&0xFF, b2),
277                         sprint_size((hopt->rate.mpu>>8)&0xFF, b3));
278                 fprintf(f, "cburst %s/%u mpu %s overhead %s ",
279                         sprint_size(cbuffer, b1),
280                         1<<hopt->ceil.cell_log,
281                         sprint_size(hopt->ceil.mpu&0xFF, b2),
282                         sprint_size((hopt->ceil.mpu>>8)&0xFF, b3));
283                 fprintf(f, "level %d ", (int)hopt->level);
284             } else {
285                 fprintf(f, "burst %s ", sprint_size(buffer, b1));
286                 fprintf(f, "cburst %s ", sprint_size(cbuffer, b1));
287             }
288             if (show_raw)
289                 fprintf(f, "buffer [%08x] cbuffer [%08x] ",
290                         hopt->buffer,hopt->cbuffer);
291         }
292         if (tb[TCA_HTB_INIT]) {
293             gopt = RTA_DATA(tb[TCA_HTB_INIT]);
294             if (RTA_PAYLOAD(tb[TCA_HTB_INIT])  < sizeof(*gopt)) return -1;
295
296             fprintf(f, "r2q %d default %x direct_packets_stat %u",
297                     gopt->rate2quantum,gopt->defcls,gopt->direct_pkts);
298                 if (show_details)
299                         fprintf(f," ver %d.%d",gopt->version >> 16,gopt->version & 0xffff);
300         }
301         return 0;
302 }
303
304 static int htb_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
305 {
306         struct tc_htb_xstats *st;
307         if (xstats == NULL)
308                 return 0;
309
310         if (RTA_PAYLOAD(xstats) < sizeof(*st))
311                 return -1;
312
313         st = RTA_DATA(xstats);
314         fprintf(f, " lended: %u borrowed: %u giants: %u\n",
315                 st->lends,st->borrows,st->giants);
316         fprintf(f, " tokens: %d ctokens: %d\n", st->tokens,st->ctokens);
317         return 0;
318 }
319
320 struct qdisc_util htb_qdisc_util = {
321         .id             = "htb",
322         .parse_qopt     = htb_parse_opt,
323         .print_qopt     = htb_print_opt,
324         .print_xstats   = htb_print_xstats,
325         .parse_copt     = htb_parse_class_opt,
326         .print_copt     = htb_print_opt,
327 };
328
329 /* for testing of old one */
330 struct qdisc_util htb2_qdisc_util = {
331         .id             =  "htb2",
332         .parse_qopt     = htb_parse_opt,
333         .print_qopt     = htb_print_opt,
334         .print_xstats   = htb_print_xstats,
335         .parse_copt     = htb_parse_class_opt,
336         .print_copt     = htb_print_opt,
337 };