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 0x001 39 #define SCH_HTB_HAS_MTU 0x002 40 #define SCH_HTB_HAS_RATE 0x004 41 #define SCH_HTB_HAS_CEIL 0x008 42 #define SCH_HTB_HAS_RBUFFER 0x010 43 #define SCH_HTB_HAS_CBUFFER 0x020 44 #define SCH_HTB_HAS_QUANTUM 0x040 45 #define SCH_HTB_HAS_OVERHEAD 0x080 46 #define SCH_HTB_HAS_MPU 0x100 49 static inline struct rtnl_htb_qdisc *htb_qdisc(
struct rtnl_qdisc *qdisc)
51 if (qdisc->q_subdata == NULL)
52 qdisc->q_subdata = calloc(1,
sizeof(
struct rtnl_htb_qdisc));
54 return (
struct rtnl_htb_qdisc *) qdisc->q_subdata;
57 static struct nla_policy htb_policy[TCA_HTB_MAX+1] = {
58 [TCA_HTB_INIT] = { .
minlen =
sizeof(
struct tc_htb_glob) },
59 [TCA_HTB_PARMS] = { .minlen =
sizeof(
struct tc_htb_opt) },
62 static int htb_qdisc_msg_parser(
struct rtnl_qdisc *qdisc)
65 struct nlattr *tb[TCA_HTB_MAX + 1];
66 struct rtnl_htb_qdisc *d;
68 err = tca_parse(tb, TCA_HTB_MAX, (
struct rtnl_tca *) qdisc, htb_policy);
74 if (tb[TCA_HTB_INIT]) {
75 struct tc_htb_glob opts;
77 nla_memcpy(&opts, tb[TCA_HTB_INIT],
sizeof(opts));
78 d->qh_rate2quantum = opts.rate2quantum;
79 d->qh_defcls = opts.defcls;
81 d->qh_mask = (SCH_HTB_HAS_RATE2QUANTUM | SCH_HTB_HAS_DEFCLS);
87 static void htb_qdisc_free_data(
struct rtnl_qdisc *qdisc)
89 free(qdisc->q_subdata);
92 static inline struct rtnl_htb_class *htb_class(
struct rtnl_class *
class)
94 if (class->c_subdata == NULL)
95 class->c_subdata = calloc(1,
sizeof(
struct rtnl_htb_class));
97 return (
struct rtnl_htb_class *)
class->c_subdata;
100 static int htb_class_msg_parser(
struct rtnl_class *
class)
103 struct nlattr *tb[TCA_HTB_MAX + 1];
104 struct rtnl_htb_class *d;
106 err = tca_parse(tb, TCA_HTB_MAX, (
struct rtnl_tca *)
class, htb_policy);
110 d = htb_class(
class);
112 if (tb[TCA_HTB_PARMS]) {
113 struct tc_htb_opt opts;
115 nla_memcpy(&opts, tb[TCA_HTB_PARMS],
sizeof(opts));
116 d->ch_prio = opts.prio;
117 rtnl_copy_ratespec(&d->ch_rate, &opts.rate);
118 rtnl_copy_ratespec(&d->ch_ceil, &opts.ceil);
121 d->ch_quantum = opts.quantum;
122 d->ch_overhead = (opts.rate.mpu >> 8) & 0xff;
123 d->ch_mpu = opts.rate.mpu & 0xff;
125 d->ch_mask = (SCH_HTB_HAS_PRIO | SCH_HTB_HAS_RATE |
126 SCH_HTB_HAS_CEIL | SCH_HTB_HAS_RBUFFER |
127 SCH_HTB_HAS_CBUFFER | SCH_HTB_HAS_QUANTUM |
128 SCH_HTB_HAS_OVERHEAD | SCH_HTB_HAS_MPU);
134 static void htb_class_free_data(
struct rtnl_class *
class)
136 free(class->c_subdata);
139 static int htb_qdisc_dump_brief(
struct rtnl_qdisc *qdisc,
142 struct rtnl_htb_qdisc *d = (
struct rtnl_htb_qdisc *) qdisc->q_subdata;
147 if (d->qh_mask & SCH_HTB_HAS_RATE2QUANTUM)
148 dp_dump(p,
" r2q %u", d->qh_rate2quantum);
150 if (d->qh_mask & SCH_HTB_HAS_DEFCLS) {
152 dp_dump(p,
" default %s",
160 static int htb_class_dump_brief(
struct rtnl_class *
class,
163 struct rtnl_htb_class *d = (
struct rtnl_htb_class *) class->c_subdata;
168 if (d->ch_mask & SCH_HTB_HAS_RATE) {
175 dp_dump(p,
" rate %.2f%s/s (%.0f%s) log %u",
176 r, ru, rbit, rubit, 1<<d->ch_rate.rs_cell_log);
183 static int htb_class_dump_full(
struct rtnl_class *
class,
186 struct rtnl_htb_class *d = (
struct rtnl_htb_class *) class->c_subdata;
192 if (d->ch_mask & SCH_HTB_HAS_CEIL) {
199 dp_dump(p,
" ceil %.2f%s/s (%.0f%s) log %u",
200 r, ru, rbit, rubit, 1<<d->ch_ceil.rs_cell_log);
203 if (d->ch_mask & SCH_HTB_HAS_PRIO)
204 dp_dump(p,
" prio %u", d->ch_prio);
206 if (d->ch_mask & SCH_HTB_HAS_MTU)
207 dp_dump(p,
" mtu %u", d->ch_mtu);
209 if (d->ch_mask & SCH_HTB_HAS_RBUFFER) {
214 dp_dump(p,
" rbuffer %.2f%s", b, bu);
217 if (d->ch_mask & SCH_HTB_HAS_CBUFFER) {
222 dp_dump(p,
" cbuffer %.2f%s", b, bu);
225 if (d->ch_mask & SCH_HTB_HAS_QUANTUM)
226 dp_dump(p,
" quantum %u", d->ch_quantum);
228 if (d->ch_mask & SCH_HTB_HAS_OVERHEAD)
229 dp_dump(p,
" overhead %u", d->ch_overhead);
231 if (d->ch_mask & SCH_HTB_HAS_MPU)
232 dp_dump(p,
" mpu %u", d->ch_mpu);
238 static struct nl_msg *htb_qdisc_get_opts(
struct rtnl_qdisc *qdisc)
240 struct rtnl_htb_qdisc *d = (
struct rtnl_htb_qdisc *) qdisc->q_subdata;
241 struct tc_htb_glob opts;
251 memset(&opts, 0,
sizeof(opts));
252 opts.version = TC_HTB_PROTOVER;
254 if (d->qh_mask & SCH_HTB_HAS_RATE2QUANTUM)
255 opts.rate2quantum = d->qh_rate2quantum;
256 if (d->qh_mask & SCH_HTB_HAS_DEFCLS)
257 opts.defcls = d->qh_defcls;
259 nla_put(msg, TCA_HTB_INIT,
sizeof(opts), &opts);
264 static uint8_t compute_cell(uint32_t rate, uint32_t mtu)
266 uint8_t cell_log = 0;
275 static struct nl_msg *htb_class_get_opts(
struct rtnl_class *
class)
277 struct rtnl_htb_class *d = (
struct rtnl_htb_class *) class->c_subdata;
279 struct tc_htb_opt opts;
282 uint8_t overhead = 0, mpu = 0;
288 memset(&opts, 0,
sizeof(opts));
291 if (d->ch_mask & SCH_HTB_HAS_PRIO)
292 opts.prio = d->ch_prio;
294 if (d->ch_mask & SCH_HTB_HAS_MTU)
299 if (!(d->ch_mask & SCH_HTB_HAS_RATE))
302 rtnl_rcopy_ratespec(&opts.rate, &d->ch_rate);
304 if (opts.rate.cell_log == UINT8_MAX)
305 opts.rate.cell_log = compute_cell(opts.rate.rate, mtu);
308 if (d->ch_mask & SCH_HTB_HAS_CEIL)
309 rtnl_rcopy_ratespec(&opts.ceil, &d->ch_ceil);
311 memcpy(&opts.ceil, &opts.rate,
sizeof(
struct tc_ratespec));
313 if (opts.ceil.cell_log == UINT8_MAX)
314 opts.ceil.cell_log = compute_cell(opts.ceil.rate, mtu);
316 if (d->ch_mask & SCH_HTB_HAS_RBUFFER)
317 buffer = d->ch_rbuffer;
319 buffer = opts.rate.rate /
nl_get_hz() + mtu;
323 if (d->ch_mask & SCH_HTB_HAS_CBUFFER)
324 cbuffer = d->ch_cbuffer;
326 cbuffer = opts.ceil.rate /
nl_get_hz() + mtu;
330 if (d->ch_mask & SCH_HTB_HAS_QUANTUM)
331 opts.quantum = d->ch_quantum;
333 if (d->ch_mask & SCH_HTB_HAS_OVERHEAD)
334 overhead = d->ch_overhead;
336 if (d->ch_mask & SCH_HTB_HAS_MPU)
339 opts.rate.mpu = mpu | (overhead << 8);
340 opts.ceil.mpu = mpu | (overhead << 8);
342 nla_put(msg, TCA_HTB_PARMS,
sizeof(opts), &opts);
345 1 << opts.rate.cell_log,
347 nla_put(msg, TCA_HTB_RTAB,
sizeof(rtable), &rtable);
350 1 << opts.ceil.cell_log,
352 nla_put(msg, TCA_HTB_CTAB,
sizeof(ctable), &ctable);
362 void rtnl_htb_set_rate2quantum(
struct rtnl_qdisc *qdisc, uint32_t rate2quantum)
364 struct rtnl_htb_qdisc *d = htb_qdisc(qdisc);
368 d->qh_rate2quantum = rate2quantum;
369 d->qh_mask |= SCH_HTB_HAS_RATE2QUANTUM;
379 struct rtnl_htb_qdisc *d = htb_qdisc(qdisc);
383 d->qh_defcls = defcls;
384 d->qh_mask |= SCH_HTB_HAS_DEFCLS;
387 void rtnl_htb_set_prio(
struct rtnl_class *
class, uint32_t prio)
389 struct rtnl_htb_class *d = htb_class(
class);
394 d->ch_mask |= SCH_HTB_HAS_PRIO;
407 struct rtnl_htb_class *d = htb_class(
class);
412 d->ch_mask |= SCH_HTB_HAS_MTU;
422 struct rtnl_htb_class *d = htb_class(
class);
426 d->ch_rate.rs_cell_log = UINT8_MAX;
427 d->ch_rate.rs_rate = rate;
428 d->ch_mask |= SCH_HTB_HAS_RATE;
438 struct rtnl_htb_class *d = htb_class(
class);
442 d->ch_ceil.rs_cell_log = UINT8_MAX;
443 d->ch_ceil.rs_rate = ceil;
444 d->ch_mask |= SCH_HTB_HAS_CEIL;
454 struct rtnl_htb_class *d = htb_class(
class);
458 d->ch_rbuffer = rbuffer;
459 d->ch_mask |= SCH_HTB_HAS_RBUFFER;
469 struct rtnl_htb_class *d = htb_class(
class);
473 d->ch_cbuffer = cbuffer;
474 d->ch_mask |= SCH_HTB_HAS_CBUFFER;
484 struct rtnl_htb_class *d = htb_class(
class);
488 d->ch_quantum = quantum;
489 d->ch_mask |= SCH_HTB_HAS_QUANTUM;
499 struct rtnl_htb_class *d = htb_class(
class);
503 d->ch_overhead = overhead;
504 d->ch_mask |= SCH_HTB_HAS_OVERHEAD;
514 struct rtnl_htb_class *d = htb_class(
class);
519 d->ch_mask |= SCH_HTB_HAS_MPU;
526 .qo_msg_parser = htb_qdisc_msg_parser,
527 .qo_free_data = htb_qdisc_free_data,
529 .qo_get_opts = htb_qdisc_get_opts,
534 .co_msg_parser = htb_class_msg_parser,
535 .co_free_data = htb_class_free_data,
538 .co_get_opts = htb_class_get_opts,
541 static void __init htb_init(
void)
547 static void __exit htb_exit(
void)
Dump object in a brief one-liner.
char qo_kind[32]
Kind/Name of Qdisc.
int rtnl_tc_calc_txtime(int bufsize, int rate)
Calculate time required to transmit buffer at a specific rate.
int rtnl_class_register(struct rtnl_class_ops *cops)
Register a class module.
void rtnl_htb_set_quantum(struct rtnl_class *class, uint32_t quantum)
Set how much bytes to serve from leaf at once of HTB class {use r2q}.
#define RTNL_TC_RTABLE_SIZE
Number of entries in a transmission time lookup table.
void rtnl_htb_set_cbuffer(struct rtnl_class *class, uint32_t cbuffer)
Set size of the ceil bucket of HTB class.
attribute validation policy
double nl_cancel_down_bits(unsigned long long l, char **unit)
Cancel down a bit counter.
int rtnl_qdisc_unregister(struct rtnl_qdisc_ops *qops)
Unregister a qdisc module.
struct nl_msg * nlmsg_alloc(void)
Allocate a new netlink message with the default maximum payload size.
void rtnl_htb_set_ceil(struct rtnl_class *class, uint32_t ceil)
Set ceil of HTB class.
void rtnl_htb_set_mpu(struct rtnl_class *class, uint8_t mpu)
Set the minimum packet size used in rate computations of HTB class.
int nla_memcpy(void *dest, struct nlattr *src, int count)
Copy a netlink attribute into another memory area.
void rtnl_htb_set_rate(struct rtnl_class *class, uint32_t rate)
Set rate of HTB class.
char * rtnl_tc_handle2str(uint32_t handle, char *buf, size_t len)
Convert a traffic control handle to a character string (Reentrant).
void rtnl_htb_set_mtu(struct rtnl_class *class, uint32_t mtu)
Set MTU of the data link.
int rtnl_tc_calc_bufsize(int txtime, int rate)
Calculate buffer size able to transmit in a specific time and rate.
double nl_cancel_down_bytes(unsigned long long l, char **unit)
Cancel down a byte counter.
int rtnl_class_unregister(struct rtnl_class_ops *cops)
Unregister a class module.
uint16_t minlen
Minimal length of payload required to be available.
int nla_put(struct nl_msg *n, int attrtype, int attrlen, const void *data)
Add a netlink attribute to a netlink message.
int rtnl_qdisc_register(struct rtnl_qdisc_ops *qops)
Register a qdisc module.
void rtnl_htb_set_overhead(struct rtnl_class *class, uint8_t overhead)
Set per-packet size overhead used in rate computations of HTB class.
int nl_get_hz(void)
Return the value of HZ.
void rtnl_htb_set_defcls(struct rtnl_qdisc *qdisc, uint32_t defcls)
Set default class of the htb qdisc to the specified value.
Dump all attributes but no statistics.
void rtnl_htb_set_rbuffer(struct rtnl_class *class, uint32_t rbuffer)
Set size of the rate bucket of HTB class.
int rtnl_tc_build_rate_table(uint32_t *dst, uint8_t mpu, uint8_t overhead, int cell, int rate)
Compute a transmission time lookup table.
char co_kind[32]
Kind/Name of class.