2 * lib/route/sch/htb.c HTB Qdisc
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
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
17 * @defgroup htb Hierachical Token Bucket (HTB)
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>
35 #define SCH_HTB_HAS_RATE2QUANTUM 0x01
36 #define SCH_HTB_HAS_DEFCLS 0x02
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
46 static inline struct rtnl_htb_qdisc *htb_qdisc(struct rtnl_qdisc *qdisc)
48 if (qdisc->q_subdata == NULL)
49 qdisc->q_subdata = calloc(1, sizeof(struct rtnl_htb_qdisc));
51 return (struct rtnl_htb_qdisc *) qdisc->q_subdata;
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) },
59 static int htb_qdisc_msg_parser(struct rtnl_qdisc *qdisc)
62 struct nlattr *tb[TCA_HTB_MAX + 1];
63 struct rtnl_htb_qdisc *d;
65 err = tca_parse(tb, TCA_HTB_MAX, (struct rtnl_tca *) qdisc, htb_policy);
71 if (tb[TCA_HTB_INIT]) {
72 struct tc_htb_glob opts;
74 nla_memcpy(&opts, tb[TCA_HTB_INIT], sizeof(opts));
75 d->qh_rate2quantum = opts.rate2quantum;
76 d->qh_defcls = opts.defcls;
78 d->qh_mask = (SCH_HTB_HAS_RATE2QUANTUM | SCH_HTB_HAS_DEFCLS);
84 static void htb_qdisc_free_data(struct rtnl_qdisc *qdisc)
86 free(qdisc->q_subdata);
89 static inline struct rtnl_htb_class *htb_class(struct rtnl_class *class)
91 if (class->c_subdata == NULL)
92 class->c_subdata = calloc(1, sizeof(struct rtnl_htb_class));
94 return (struct rtnl_htb_class *) class->c_subdata;
97 static int htb_class_msg_parser(struct rtnl_class *class)
100 struct nlattr *tb[TCA_HTB_MAX + 1];
101 struct rtnl_htb_class *d;
103 err = tca_parse(tb, TCA_HTB_MAX, (struct rtnl_tca *) class, htb_policy);
107 d = htb_class(class);
109 if (tb[TCA_HTB_PARMS]) {
110 struct tc_htb_opt opts;
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;
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);
127 static void htb_class_free_data(struct rtnl_class *class)
129 free(class->c_subdata);
132 static int htb_qdisc_dump_brief(struct rtnl_qdisc *qdisc,
133 struct nl_dump_params *p, int line)
135 struct rtnl_htb_qdisc *d = (struct rtnl_htb_qdisc *) qdisc->q_subdata;
140 if (d->qh_mask & SCH_HTB_HAS_RATE2QUANTUM)
141 dp_dump(p, " r2q %u", d->qh_rate2quantum);
143 if (d->qh_mask & SCH_HTB_HAS_DEFCLS) {
145 dp_dump(p, " default %s",
146 rtnl_tc_handle2str(d->qh_defcls, buf, sizeof(buf)));
153 static int htb_class_dump_brief(struct rtnl_class *class,
154 struct nl_dump_params *p, int line)
156 struct rtnl_htb_class *d = (struct rtnl_htb_class *) class->c_subdata;
161 if (d->ch_mask & SCH_HTB_HAS_RATE) {
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);
168 dp_dump(p, " rate %.2f%s/s (%.0f%s) log %u",
169 r, ru, rbit, rubit, 1<<d->ch_rate.rs_cell_log);
176 static int htb_class_dump_full(struct rtnl_class *class,
177 struct nl_dump_params *p, int line)
179 struct rtnl_htb_class *d = (struct rtnl_htb_class *) class->c_subdata;
185 if (d->ch_mask & SCH_HTB_HAS_CEIL) {
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);
192 dp_dump(p, " ceil %.2f%s/s (%.0f%s) log %u",
193 r, ru, rbit, rubit, 1<<d->ch_ceil.rs_cell_log);
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);
207 static struct nl_msg *htb_qdisc_get_opts(struct rtnl_qdisc *qdisc)
209 struct rtnl_htb_qdisc *d = (struct rtnl_htb_qdisc *) qdisc->q_subdata;
210 struct tc_htb_glob opts;
216 msg = nlmsg_build(NULL);
220 memset(&opts, 0, sizeof(opts));
221 opts.version = TC_HTB_PROTOVER;
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;
228 nla_put(msg, TCA_HTB_INIT, sizeof(opts), &opts);
233 static inline uint32_t compute_burst(uint32_t rate, uint32_t mtu)
235 return rtnl_tc_calc_txtime(rate / nl_get_hz() + mtu, rate);
238 static uint8_t compute_cell(uint32_t rate, uint32_t mtu)
240 uint8_t cell_log = 0;
249 static struct nl_msg *htb_class_get_opts(struct rtnl_class *class)
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;
260 msg = nlmsg_build(NULL);
261 memset(&opts, 0, sizeof(opts));
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;
267 if (!(d->ch_mask & SCH_HTB_HAS_RATE))
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)
274 if(!(d->ch_mask & SCH_HTB_HAS_MTU))
276 opts.rate.cell_log = compute_cell(opts.rate.rate, d->ch_mtu);
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);
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)
287 if(!(d->ch_mask & SCH_HTB_HAS_MTU))
289 opts.ceil.cell_log = compute_cell(opts.ceil.rate, d->ch_mtu);
292 if (d->ch_mask & SCH_HTB_HAS_RBUFFER)
293 opts.buffer = d->ch_rbuffer;
296 if(!(d->ch_mask & SCH_HTB_HAS_MTU))
298 opts.buffer = compute_burst(opts.rate.rate, d->ch_mtu);
301 if (d->ch_mask & SCH_HTB_HAS_CBUFFER)
302 opts.cbuffer = d->ch_cbuffer;
305 if(!(d->ch_mask & SCH_HTB_HAS_MTU))
307 opts.cbuffer = compute_burst(opts.ceil.rate, d->ch_mtu);
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,
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,
318 nla_put(msg, TCA_HTB_CTAB, sizeof(ctable), &ctable);
324 * @name Attribute Modifications
328 void rtnl_htb_set_rate2quantum(struct rtnl_qdisc *qdisc, uint32_t rate2quantum)
330 struct rtnl_htb_qdisc *d = htb_qdisc(qdisc);
334 d->qh_rate2quantum = rate2quantum;
335 d->qh_mask |= SCH_HTB_HAS_RATE2QUANTUM;
339 * Set default class of the htb qdisc to the specified value
340 * @arg qdisc qdisc to change
341 * @arg defcls new default class
343 void rtnl_htb_set_defcls(struct rtnl_qdisc *qdisc, uint32_t defcls)
345 struct rtnl_htb_qdisc *d = htb_qdisc(qdisc);
349 d->qh_defcls = defcls;
350 d->qh_mask |= SCH_HTB_HAS_DEFCLS;
353 void rtnl_htb_set_prio(struct rtnl_class *class, uint32_t prio)
355 struct rtnl_htb_class *d = htb_class(class);
360 d->ch_mask |= SCH_HTB_HAS_PRIO;
363 void rtnl_htb_set_mtu(struct rtnl_class *class, uint32_t mtu)
365 struct rtnl_htb_class *d = htb_class(class);
370 d->ch_mask |= SCH_HTB_HAS_MTU;
373 void rtnl_htb_set_rate(struct rtnl_class *class, uint32_t rate)
375 struct rtnl_htb_class *d = htb_class(class);
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;
384 void rtnl_htb_set_ceil(struct rtnl_class *class, uint32_t ceil)
386 struct rtnl_htb_class *d = htb_class(class);
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;
397 static struct rtnl_qdisc_ops htb_qdisc_ops = {
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,
405 static struct rtnl_class_ops htb_class_ops = {
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,
414 static void __init htb_init(void)
416 rtnl_qdisc_register(&htb_qdisc_ops);
417 rtnl_class_register(&htb_class_ops);
420 static void __exit htb_exit(void)
422 rtnl_qdisc_unregister(&htb_qdisc_ops);
423 rtnl_class_unregister(&htb_class_ops);