oops
[libnl.git] / lib / route / sch / htb.c
1 /*
2  * lib/route/sch/htb.c  HTB Qdisc
3  *
4  *      This library is free software; you can redistribute it and/or
5  *      modify it under the terms of the GNU Lesser General Public
6  *      License as published by the Free Software Foundation version 2.1
7  *      of the License.
8  *
9  * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
10  * Copyright (c) 2005-2006 Petr Gotthard <petr.gotthard@siemens.com>
11  * Copyright (c) 2005-2006 Siemens AG Oesterreich
12  */
13
14 /**
15  * @ingroup qdisc
16  * @ingroup class
17  * @defgroup htb Hierachical Token Bucket (HTB)
18  * @{
19  */
20
21 #include <netlink-local.h>
22 #include <netlink-tc.h>
23 #include <netlink/netlink.h>
24 #include <netlink/cache.h>
25 #include <netlink/utils.h>
26 #include <netlink/route/tc.h>
27 #include <netlink/route/qdisc.h>
28 #include <netlink/route/qdisc-modules.h>
29 #include <netlink/route/class.h>
30 #include <netlink/route/class-modules.h>
31 #include <netlink/route/link.h>
32 #include <netlink/route/sch/htb.h>
33
34 /** @cond SKIP */
35 #define SCH_HTB_HAS_RATE2QUANTUM        0x01
36 #define SCH_HTB_HAS_DEFCLS              0x02
37
38 #define SCH_HTB_HAS_PRIO                0x01
39 #define SCH_HTB_HAS_MTU                 0x02
40 #define SCH_HTB_HAS_RATE                0x04
41 #define SCH_HTB_HAS_CEIL                0x08
42 #define SCH_HTB_HAS_RBUFFER             0x10
43 #define SCH_HTB_HAS_CBUFFER             0x20
44 /** @endcond */
45
46 static inline struct rtnl_htb_qdisc *htb_qdisc(struct rtnl_qdisc *qdisc)
47 {
48         if (qdisc->q_subdata == NULL)
49                 qdisc->q_subdata = calloc(1, sizeof(struct rtnl_htb_qdisc));
50
51         return (struct rtnl_htb_qdisc *) qdisc->q_subdata;
52 }
53
54 static struct nla_policy htb_policy[TCA_HTB_MAX+1] = {
55         [TCA_HTB_INIT]  = { .minlen = sizeof(struct tc_htb_glob) },
56         [TCA_HTB_PARMS] = { .minlen = sizeof(struct tc_htb_opt) },
57 };
58
59 static int htb_qdisc_msg_parser(struct rtnl_qdisc *qdisc)
60 {
61         int err;
62         struct nlattr *tb[TCA_HTB_MAX + 1];
63         struct rtnl_htb_qdisc *d;
64
65         err = tca_parse(tb, TCA_HTB_MAX, (struct rtnl_tca *) qdisc, htb_policy);
66         if (err < 0)
67                 return err;
68         
69         d = htb_qdisc(qdisc);
70
71         if (tb[TCA_HTB_INIT]) {
72                 struct tc_htb_glob opts;
73
74                 nla_memcpy(&opts, tb[TCA_HTB_INIT], sizeof(opts));
75                 d->qh_rate2quantum = opts.rate2quantum;
76                 d->qh_defcls = opts.defcls;
77
78                 d->qh_mask = (SCH_HTB_HAS_RATE2QUANTUM | SCH_HTB_HAS_DEFCLS);
79         }
80
81         return 0;
82 }
83
84 static void htb_qdisc_free_data(struct rtnl_qdisc *qdisc)
85 {
86         free(qdisc->q_subdata);
87 }
88
89 static inline struct rtnl_htb_class *htb_class(struct rtnl_class *class)
90 {
91         if (class->c_subdata == NULL)
92                 class->c_subdata = calloc(1, sizeof(struct rtnl_htb_class));
93
94         return (struct rtnl_htb_class *) class->c_subdata;
95 }
96
97 static int htb_class_msg_parser(struct rtnl_class *class)
98 {
99         int err;
100         struct nlattr *tb[TCA_HTB_MAX + 1];
101         struct rtnl_htb_class *d;
102
103         err = tca_parse(tb, TCA_HTB_MAX, (struct rtnl_tca *) class, htb_policy);
104         if (err < 0)
105                 return err;
106         
107         d = htb_class(class);
108
109         if (tb[TCA_HTB_PARMS]) {
110                 struct tc_htb_opt opts;
111
112                 nla_memcpy(&opts, tb[TCA_HTB_PARMS], sizeof(opts));
113                 d->ch_prio = opts.prio;
114                 rtnl_copy_ratespec(&d->ch_rate, &opts.rate);
115                 rtnl_copy_ratespec(&d->ch_ceil, &opts.ceil);
116                 d->ch_rbuffer = opts.buffer;
117                 d->ch_cbuffer = opts.cbuffer;
118
119                 d->ch_mask = (SCH_HTB_HAS_PRIO | SCH_HTB_HAS_RATE |
120                         SCH_HTB_HAS_CEIL | SCH_HTB_HAS_RBUFFER |
121                         SCH_HTB_HAS_CBUFFER);
122         }
123
124         return 0;
125 }
126
127 static void htb_class_free_data(struct rtnl_class *class)
128 {
129         free(class->c_subdata);
130 }
131
132 static int htb_qdisc_dump_brief(struct rtnl_qdisc *qdisc,
133                                 struct nl_dump_params *p, int line)
134 {
135         struct rtnl_htb_qdisc *d = (struct rtnl_htb_qdisc *) qdisc->q_subdata;
136
137         if (d == NULL)
138                 goto ignore;
139
140         if (d->qh_mask & SCH_HTB_HAS_RATE2QUANTUM)
141                 dp_dump(p, " r2q %u", d->qh_rate2quantum);
142
143         if (d->qh_mask & SCH_HTB_HAS_DEFCLS) {
144                 char buf[32];
145                 dp_dump(p, " default %s",
146                         rtnl_tc_handle2str(d->qh_defcls, buf, sizeof(buf)));
147         }
148
149 ignore:
150         return line;
151 }
152
153 static int htb_class_dump_brief(struct rtnl_class *class,
154                                 struct nl_dump_params *p, int line)
155 {
156         struct rtnl_htb_class *d = (struct rtnl_htb_class *) class->c_subdata;
157
158         if (d == NULL)
159                 goto ignore;
160
161         if (d->ch_mask & SCH_HTB_HAS_RATE) {
162                 double r, rbit;
163                 char *ru, *rubit;
164
165                 r = nl_cancel_down_bytes(d->ch_rate.rs_rate, &ru);
166                 rbit = nl_cancel_down_bits(d->ch_rate.rs_rate*8, &rubit);
167
168                 dp_dump(p, " rate %.2f%s/s (%.0f%s) log %u",
169                         r, ru, rbit, rubit, 1<<d->ch_rate.rs_cell_log);
170         }
171
172 ignore:
173         return line;
174 }
175
176 static int htb_class_dump_full(struct rtnl_class *class,
177                                struct nl_dump_params *p, int line)
178 {
179         struct rtnl_htb_class *d = (struct rtnl_htb_class *) class->c_subdata;
180
181         if (d == NULL)
182                 goto ignore;
183
184         /* line 1 */
185         if (d->ch_mask & SCH_HTB_HAS_CEIL) {
186                 double r, rbit;
187                 char *ru, *rubit;
188
189                 r = nl_cancel_down_bytes(d->ch_ceil.rs_rate, &ru);
190                 rbit = nl_cancel_down_bits(d->ch_ceil.rs_rate*8, &rubit);
191
192                 dp_dump(p, "    ceil %.2f%s/s (%.0f%s) log %u",
193                         r, ru, rbit, rubit, 1<<d->ch_ceil.rs_cell_log);
194         }
195
196         if (d->ch_mask & SCH_HTB_HAS_PRIO)
197                 dp_dump(p, " prio %u", d->ch_prio);
198         if (d->ch_mask & SCH_HTB_HAS_RBUFFER)
199                 dp_dump(p, " rbuffer %u", d->ch_rbuffer);
200         if (d->ch_mask & SCH_HTB_HAS_CBUFFER)
201                 dp_dump(p, " cbuffer %u", d->ch_cbuffer);
202
203 ignore:
204         return line;
205 }
206
207 static struct nl_msg *htb_qdisc_get_opts(struct rtnl_qdisc *qdisc)
208 {
209         struct rtnl_htb_qdisc *d = (struct rtnl_htb_qdisc *) qdisc->q_subdata;
210         struct tc_htb_glob opts;
211         struct nl_msg *msg;
212
213         if (d == NULL)
214                 return NULL;
215
216         msg = nlmsg_build(NULL);
217         if (msg == NULL)
218                 return NULL;
219
220         memset(&opts, 0, sizeof(opts));
221         opts.version = TC_HTB_PROTOVER;
222
223         if (d->qh_mask & SCH_HTB_HAS_RATE2QUANTUM)
224                 opts.rate2quantum = d->qh_rate2quantum;
225         if (d->qh_mask & SCH_HTB_HAS_DEFCLS)
226                 opts.defcls = d->qh_defcls;
227
228         nla_put(msg, TCA_HTB_INIT, sizeof(opts), &opts);
229
230         return msg;
231 }
232
233 static inline uint32_t compute_burst(uint32_t rate, uint32_t mtu)
234 {
235         return rtnl_tc_calc_txtime(rate / nl_get_hz() + mtu, rate);
236 }
237
238 static uint8_t compute_cell(uint32_t rate, uint32_t mtu)
239 {
240         uint8_t cell_log = 0;
241         while (mtu > 255) {
242                 mtu >>= 1;
243                 cell_log++;
244         }
245
246         return cell_log;
247 }
248
249 static struct nl_msg *htb_class_get_opts(struct rtnl_class *class)
250 {
251         struct rtnl_htb_class *d = (struct rtnl_htb_class *) class->c_subdata;
252         uint32_t rtable[256], ctable[256];
253         struct tc_htb_opt opts;
254         struct nl_msg *msg;
255         
256
257         if (d == NULL)
258                 return NULL;
259
260         msg = nlmsg_build(NULL);
261         memset(&opts, 0, sizeof(opts));
262
263         /* if not set, zero (0) is used as priority */
264         if (d->ch_mask & SCH_HTB_HAS_PRIO)
265                 opts.prio = d->ch_prio;
266
267         if (!(d->ch_mask & SCH_HTB_HAS_RATE))
268                 BUG();
269
270         rtnl_rcopy_ratespec(&opts.rate, &d->ch_rate);
271         /* if cell_log not set, compute default value */
272         if (opts.rate.cell_log == UINT8_MAX)
273         {
274                 if(!(d->ch_mask & SCH_HTB_HAS_MTU))
275                         BUG();
276                 opts.rate.cell_log = compute_cell(opts.rate.rate, d->ch_mtu);
277         }
278
279         /* if not set, configured rate is used as ceil, which implies no borrowing */
280         if (d->ch_mask & SCH_HTB_HAS_CEIL)
281                 rtnl_rcopy_ratespec(&opts.ceil, &d->ch_ceil);
282         else
283                 memcpy(&opts.ceil, &opts.rate, sizeof(struct tc_ratespec));
284         /* if cell_log not set, compute default value */
285         if (opts.ceil.cell_log == UINT8_MAX)
286         {
287                 if(!(d->ch_mask & SCH_HTB_HAS_MTU))
288                         BUG();
289                 opts.ceil.cell_log = compute_cell(opts.ceil.rate, d->ch_mtu);
290         }
291
292         if (d->ch_mask & SCH_HTB_HAS_RBUFFER)
293                 opts.buffer = d->ch_rbuffer;
294         else
295         {
296                 if(!(d->ch_mask & SCH_HTB_HAS_MTU))
297                         BUG();
298                 opts.buffer = compute_burst(opts.rate.rate, d->ch_mtu);
299         }
300
301         if (d->ch_mask & SCH_HTB_HAS_CBUFFER)
302                 opts.cbuffer = d->ch_cbuffer;
303         else
304         {
305                 if(!(d->ch_mask & SCH_HTB_HAS_MTU))
306                         BUG();
307                 opts.cbuffer = compute_burst(opts.ceil.rate, d->ch_mtu);
308         }
309
310         nla_put(msg, TCA_HTB_PARMS, sizeof(opts), &opts);
311         rtnl_tc_build_rate_table(rtable, opts.rate.mpu & 0xff,
312                                  opts.rate.mpu >> 8, 1 << opts.rate.cell_log,
313                                  opts.rate.rate);
314         nla_put(msg, TCA_HTB_RTAB, sizeof(rtable), &rtable);
315         rtnl_tc_build_rate_table(ctable, opts.ceil.mpu & 0xff,
316                                  opts.ceil.mpu >> 8, 1 << opts.ceil.cell_log,
317                                  opts.ceil.rate);
318         nla_put(msg, TCA_HTB_CTAB, sizeof(ctable), &ctable);
319
320         return msg;
321 }
322
323 /**
324  * @name Attribute Modifications
325  * @{
326  */
327
328 void rtnl_htb_set_rate2quantum(struct rtnl_qdisc *qdisc, uint32_t rate2quantum)
329 {
330         struct rtnl_htb_qdisc *d = htb_qdisc(qdisc);
331         if (d == NULL)
332                 return;
333
334         d->qh_rate2quantum = rate2quantum;
335         d->qh_mask |= SCH_HTB_HAS_RATE2QUANTUM;
336 }
337
338 /**
339  * Set default class of the htb qdisc to the specified value
340  * @arg qdisc           qdisc to change
341  * @arg defcls          new default class
342  */
343 void rtnl_htb_set_defcls(struct rtnl_qdisc *qdisc, uint32_t defcls)
344 {
345         struct rtnl_htb_qdisc *d = htb_qdisc(qdisc);
346         if (d == NULL)
347                 return;
348
349         d->qh_defcls = defcls;
350         d->qh_mask |= SCH_HTB_HAS_DEFCLS;
351 }
352
353 void rtnl_htb_set_prio(struct rtnl_class *class, uint32_t prio)
354 {
355         struct rtnl_htb_class *d = htb_class(class);
356         if (d == NULL)
357                 return;
358
359         d->ch_prio = prio;
360         d->ch_mask |= SCH_HTB_HAS_PRIO;
361 }
362
363 void rtnl_htb_set_mtu(struct rtnl_class *class, uint32_t mtu)
364 {
365         struct rtnl_htb_class *d = htb_class(class);
366         if (d == NULL)
367                 return;
368
369         d->ch_mtu = mtu;
370         d->ch_mask |= SCH_HTB_HAS_MTU;
371 }
372
373 void rtnl_htb_set_rate(struct rtnl_class *class, uint32_t rate)
374 {
375         struct rtnl_htb_class *d = htb_class(class);
376         if (d == NULL)
377                 return;
378
379         d->ch_rate.rs_cell_log = UINT8_MAX; /* use default value */
380         d->ch_rate.rs_rate = rate;
381         d->ch_mask |= SCH_HTB_HAS_RATE;
382 }
383
384 void rtnl_htb_set_ceil(struct rtnl_class *class, uint32_t ceil)
385 {
386         struct rtnl_htb_class *d = htb_class(class);
387         if (d == NULL)
388                 return;
389
390         d->ch_ceil.rs_cell_log = UINT8_MAX; /* use default value */
391         d->ch_ceil.rs_rate = ceil;
392         d->ch_mask |= SCH_HTB_HAS_CEIL;
393 }
394
395 /** @} */
396
397 static struct rtnl_qdisc_ops htb_qdisc_ops = {
398         .qo_kind                = "htb",
399         .qo_msg_parser          = htb_qdisc_msg_parser,
400         .qo_free_data           = htb_qdisc_free_data,
401         .qo_dump[NL_DUMP_BRIEF] = htb_qdisc_dump_brief,
402         .qo_get_opts            = htb_qdisc_get_opts,
403 };
404
405 static struct rtnl_class_ops htb_class_ops = {
406         .co_kind                = "htb",
407         .co_msg_parser          = htb_class_msg_parser,
408         .co_free_data           = htb_class_free_data,
409         .co_dump[NL_DUMP_BRIEF] = htb_class_dump_brief,
410         .co_dump[NL_DUMP_FULL]  = htb_class_dump_full,
411         .co_get_opts            = htb_class_get_opts,
412 };
413
414 static void __init htb_init(void)
415 {
416         rtnl_qdisc_register(&htb_qdisc_ops);
417         rtnl_class_register(&htb_class_ops);
418 }
419
420 static void __exit htb_exit(void)
421 {
422         rtnl_qdisc_unregister(&htb_qdisc_ops);
423         rtnl_class_unregister(&htb_class_ops);
424 }
425
426 /** @} */