1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
/*
* lib/route/sch/cbq.c Class Based Queueing
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
*/
#include <netlink-local.h>
#include <netlink-tc.h>
#include <netlink/netlink.h>
#include <netlink/utils.h>
#include <netlink/route/qdisc.h>
#include <netlink/route/qdisc-modules.h>
#include <netlink/route/class.h>
#include <netlink/route/class-modules.h>
#include <netlink/route/link.h>
#include <netlink/route/sch/cbq.h>
#include <netlink/route/cls/police.h>
/**
* @ingroup qdisc_api
* @ingroup class_api
* @defgroup cbq Class Based Queueing (CBQ)
* @{
*/
static const struct trans_tbl ovl_strategies[] = {
__ADD(TC_CBQ_OVL_CLASSIC,classic)
__ADD(TC_CBQ_OVL_DELAY,delay)
__ADD(TC_CBQ_OVL_LOWPRIO,lowprio)
__ADD(TC_CBQ_OVL_DROP,drop)
__ADD(TC_CBQ_OVL_RCLASSIC,rclassic)
};
/**
* Convert a CBQ OVL strategy to a character string
* @arg type CBQ OVL strategy
* @arg buf destination buffer
* @arg len length of destination buffer
*
* Converts a CBQ OVL strategy to a character string and stores in the
* provided buffer. Returns the destination buffer or the type
* encoded in hex if no match was found.
*/
char *nl_ovl_strategy2str(int type, char *buf, size_t len)
{
return __type2str(type, buf, len, ovl_strategies,
ARRAY_SIZE(ovl_strategies));
}
/**
* Convert a string to a CBQ OVL strategy
* @arg name CBQ OVL stragegy name
*
* Converts a CBQ OVL stragegy name to it's corresponding CBQ OVL strategy
* type. Returns the type or -1 if none was found.
*/
int nl_str2ovl_strategy(const char *name)
{
return __str2type(name, ovl_strategies, ARRAY_SIZE(ovl_strategies));
}
static struct nla_policy cbq_policy[TCA_CBQ_MAX+1] = {
[TCA_CBQ_LSSOPT] = { .minlen = sizeof(struct tc_cbq_lssopt) },
[TCA_CBQ_RATE] = { .minlen = sizeof(struct tc_ratespec) },
[TCA_CBQ_WRROPT] = { .minlen = sizeof(struct tc_cbq_wrropt) },
[TCA_CBQ_OVL_STRATEGY] = { .minlen = sizeof(struct tc_cbq_ovl) },
[TCA_CBQ_FOPT] = { .minlen = sizeof(struct tc_cbq_fopt) },
[TCA_CBQ_POLICE] = { .minlen = sizeof(struct tc_cbq_police) },
};
static inline struct rtnl_cbq *cbq_qdisc(struct rtnl_tc *tca)
{
return (struct rtnl_cbq *) tca->tc_subdata;
}
static inline struct rtnl_cbq *cbq_alloc(struct rtnl_tc *tca)
{
if (!tca->tc_subdata)
tca->tc_subdata = calloc(1, sizeof(struct rtnl_qdisc));
return cbq_qdisc(tca);
}
static int cbq_msg_parser(struct rtnl_tc *tca)
{
struct nlattr *tb[TCA_CBQ_MAX + 1];
struct rtnl_cbq *cbq;
int err;
err = tca_parse(tb, TCA_CBQ_MAX, tca, cbq_policy);
if (err < 0)
return err;
cbq = cbq_alloc(tca);
if (!cbq)
return -NLE_NOMEM;
nla_memcpy(&cbq->cbq_lss, tb[TCA_CBQ_LSSOPT], sizeof(cbq->cbq_lss));
nla_memcpy(&cbq->cbq_rate, tb[TCA_CBQ_RATE], sizeof(cbq->cbq_rate));
nla_memcpy(&cbq->cbq_wrr, tb[TCA_CBQ_WRROPT], sizeof(cbq->cbq_wrr));
nla_memcpy(&cbq->cbq_fopt, tb[TCA_CBQ_FOPT], sizeof(cbq->cbq_fopt));
nla_memcpy(&cbq->cbq_ovl, tb[TCA_CBQ_OVL_STRATEGY],
sizeof(cbq->cbq_ovl));
nla_memcpy(&cbq->cbq_police, tb[TCA_CBQ_POLICE],
sizeof(cbq->cbq_police));
return 0;
}
static int cbq_qdisc_msg_parser(struct rtnl_qdisc *qdisc)
{
return cbq_msg_parser((struct rtnl_tc *) qdisc);
}
static int cbq_class_msg_parser(struct rtnl_class *class)
{
return cbq_msg_parser((struct rtnl_tc *) class);
}
static void cbq_qdisc_free_data(struct rtnl_qdisc *qdisc)
{
free(qdisc->q_subdata);
}
static int cbq_clone(struct rtnl_tc *_dst, struct rtnl_tc *_src)
{
struct rtnl_cbq *src = cbq_qdisc(_src);
if (src && !cbq_alloc(_dst))
return -NLE_NOMEM;
else
return 0;
}
static int cbq_qdisc_clone(struct rtnl_qdisc *dst, struct rtnl_qdisc *src)
{
return cbq_clone((struct rtnl_tc *) dst, (struct rtnl_tc *) src);
}
static void cbq_class_free_data(struct rtnl_class *class)
{
free(class->c_subdata);
}
static int cbq_class_clone(struct rtnl_class *dst, struct rtnl_class *src)
{
return cbq_clone((struct rtnl_tc *) dst, (struct rtnl_tc *) src);
}
static void cbq_dump_line(struct rtnl_tc *tca, struct nl_dump_params *p)
{
struct rtnl_cbq *cbq;
double r, rbit;
char *ru, *rubit;
cbq = cbq_qdisc(tca);
if (!cbq)
return;
r = nl_cancel_down_bytes(cbq->cbq_rate.rate, &ru);
rbit = nl_cancel_down_bits(cbq->cbq_rate.rate * 8, &rubit);
nl_dump(p, " rate %.2f%s/s (%.0f%s) prio %u",
r, ru, rbit, rubit, cbq->cbq_wrr.priority);
}
static void cbq_qdisc_dump_line(struct rtnl_qdisc *qdisc,
struct nl_dump_params *p)
{
cbq_dump_line((struct rtnl_tc *) qdisc, p);
}
static void cbq_class_dump_line(struct rtnl_class *class,
struct nl_dump_params *p)
{
cbq_dump_line((struct rtnl_tc *) class, p);
}
static void cbq_dump_details(struct rtnl_tc *tca, struct nl_dump_params *p)
{
struct rtnl_cbq *cbq;
char *unit, buf[32];
double w;
uint32_t el;
cbq = cbq_qdisc(tca);
if (!cbq)
return;
w = nl_cancel_down_bits(cbq->cbq_wrr.weight * 8, &unit);
nl_dump(p, "avgpkt %u mpu %u cell %u allot %u weight %.0f%s\n",
cbq->cbq_lss.avpkt,
cbq->cbq_rate.mpu,
1 << cbq->cbq_rate.cell_log,
cbq->cbq_wrr.allot, w, unit);
el = cbq->cbq_lss.ewma_log;
nl_dump_line(p, " minidle %uus maxidle %uus offtime "
"%uus level %u ewma_log %u\n",
nl_ticks2us(cbq->cbq_lss.minidle >> el),
nl_ticks2us(cbq->cbq_lss.maxidle >> el),
nl_ticks2us(cbq->cbq_lss.offtime >> el),
cbq->cbq_lss.level,
cbq->cbq_lss.ewma_log);
nl_dump_line(p, " penalty %uus strategy %s ",
nl_ticks2us(cbq->cbq_ovl.penalty),
nl_ovl_strategy2str(cbq->cbq_ovl.strategy, buf, sizeof(buf)));
nl_dump(p, "split %s defmap 0x%08x ",
rtnl_tc_handle2str(cbq->cbq_fopt.split, buf, sizeof(buf)),
cbq->cbq_fopt.defmap);
nl_dump(p, "police %s",
nl_police2str(cbq->cbq_police.police, buf, sizeof(buf)));
}
static void cbq_qdisc_dump_details(struct rtnl_qdisc *qdisc,
struct nl_dump_params *p)
{
cbq_dump_details((struct rtnl_tc *) qdisc, p);
}
static void cbq_class_dump_details(struct rtnl_class *class,
struct nl_dump_params *p)
{
cbq_dump_details((struct rtnl_tc *) class, p);
}
static void cbq_dump_stats(struct rtnl_tc *tca, struct nl_dump_params *p)
{
struct tc_cbq_xstats *x = tca_xstats(tca);
if (!x)
return;
nl_dump_line(p, " borrows overact "
" avgidle undertime\n");
nl_dump_line(p, " %10u %10u %10u %10u\n",
x->borrows, x->overactions, x->avgidle, x->undertime);
}
static void cbq_qdisc_dump_stats(struct rtnl_qdisc *qdisc,
struct nl_dump_params *p)
{
cbq_dump_stats((struct rtnl_tc *) qdisc, p);
}
static void cbq_class_dump_stats(struct rtnl_class *class,
struct nl_dump_params *p)
{
cbq_dump_stats((struct rtnl_tc *) class, p);
}
static struct rtnl_qdisc_ops cbq_qdisc_ops = {
.qo_kind = "cbq",
.qo_msg_parser = cbq_qdisc_msg_parser,
.qo_free_data = cbq_qdisc_free_data,
.qo_clone = cbq_qdisc_clone,
.qo_dump = {
[NL_DUMP_LINE] = cbq_qdisc_dump_line,
[NL_DUMP_DETAILS] = cbq_qdisc_dump_details,
[NL_DUMP_STATS] = cbq_qdisc_dump_stats,
},
};
static struct rtnl_class_ops cbq_class_ops = {
.co_kind = "cbq",
.co_msg_parser = cbq_class_msg_parser,
.co_free_data = cbq_class_free_data,
.co_clone = cbq_class_clone,
.co_dump = {
[NL_DUMP_LINE] = cbq_class_dump_line,
[NL_DUMP_DETAILS] = cbq_class_dump_details,
[NL_DUMP_STATS] = cbq_class_dump_stats,
},
};
static void __init cbq_init(void)
{
rtnl_qdisc_register(&cbq_qdisc_ops);
rtnl_class_register(&cbq_class_ops);
}
static void __exit cbq_exit(void)
{
rtnl_qdisc_unregister(&cbq_qdisc_ops);
rtnl_class_unregister(&cbq_class_ops);
}
/** @} */
|