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
|
/*
* lib/route/qdisc/prio.c PRIO Qdisc/Class
*
* 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-2011 Thomas Graf <tgraf@suug.ch>
*/
/**
* @ingroup qdisc
* @defgroup qdisc_prio (Fast) Prio
* @brief
*
* @par 1) Typical PRIO configuration
* @code
* // Specify the maximal number of bands to be used for this PRIO qdisc.
* rtnl_qdisc_prio_set_bands(qdisc, QDISC_PRIO_DEFAULT_BANDS);
*
* // Provide a map assigning each priority to a band number.
* uint8_t map[] = QDISC_PRIO_DEFAULT_PRIOMAP;
* rtnl_qdisc_prio_set_priomap(qdisc, map, sizeof(map));
* @endcode
* @{
*/
#include <netlink-private/netlink.h>
#include <netlink-private/tc.h>
#include <netlink/netlink.h>
#include <netlink/utils.h>
#include <netlink-private/route/tc-api.h>
#include <netlink/route/qdisc.h>
#include <netlink/route/qdisc/prio.h>
/** @cond SKIP */
#define SCH_PRIO_ATTR_BANDS 1
#define SCH_PRIO_ATTR_PRIOMAP 2
/** @endcond */
static int prio_msg_parser(struct rtnl_tc *tc, void *data)
{
struct rtnl_prio *prio = data;
struct tc_prio_qopt *opt;
if (tc->tc_opts->d_size < sizeof(*opt))
return -NLE_INVAL;
opt = (struct tc_prio_qopt *) tc->tc_opts->d_data;
prio->qp_bands = opt->bands;
memcpy(prio->qp_priomap, opt->priomap, sizeof(prio->qp_priomap));
prio->qp_mask = (SCH_PRIO_ATTR_BANDS | SCH_PRIO_ATTR_PRIOMAP);
return 0;
}
static void prio_dump_line(struct rtnl_tc *tc, void *data,
struct nl_dump_params *p)
{
struct rtnl_prio *prio = data;
if (prio)
nl_dump(p, " bands %u", prio->qp_bands);
}
static void prio_dump_details(struct rtnl_tc *tc, void *data,
struct nl_dump_params *p)
{
struct rtnl_prio *prio = data;
int i, hp;
if (!prio)
return;
nl_dump(p, "priomap [");
for (i = 0; i <= TC_PRIO_MAX; i++)
nl_dump(p, "%u%s", prio->qp_priomap[i],
i < TC_PRIO_MAX ? " " : "");
nl_dump(p, "]\n");
nl_new_line(p);
hp = (((TC_PRIO_MAX/2) + 1) & ~1);
for (i = 0; i < hp; i++) {
char a[32];
nl_dump(p, " %18s => %u",
rtnl_prio2str(i, a, sizeof(a)),
prio->qp_priomap[i]);
if (hp+i <= TC_PRIO_MAX) {
nl_dump(p, " %18s => %u",
rtnl_prio2str(hp+i, a, sizeof(a)),
prio->qp_priomap[hp+i]);
if (i < (hp - 1)) {
nl_dump(p, "\n");
nl_new_line(p);
}
}
}
}
static int prio_msg_fill(struct rtnl_tc *tc, void *data, struct nl_msg *msg)
{
struct rtnl_prio *prio = data;
struct tc_prio_qopt opts;
if (!prio || !(prio->qp_mask & SCH_PRIO_ATTR_PRIOMAP))
BUG();
opts.bands = prio->qp_bands;
memcpy(opts.priomap, prio->qp_priomap, sizeof(opts.priomap));
return nlmsg_append(msg, &opts, sizeof(opts), NL_DONTPAD);
}
/**
* @name Attribute Modification
* @{
*/
/**
* Set number of bands of PRIO qdisc.
* @arg qdisc PRIO qdisc to be modified.
* @arg bands New number of bands.
* @return 0 on success or a negative error code.
*/
void rtnl_qdisc_prio_set_bands(struct rtnl_qdisc *qdisc, int bands)
{
struct rtnl_prio *prio;
if (!(prio = rtnl_tc_data(TC_CAST(qdisc))))
BUG();
prio->qp_bands = bands;
prio->qp_mask |= SCH_PRIO_ATTR_BANDS;
}
/**
* Get number of bands of PRIO qdisc.
* @arg qdisc PRIO qdisc.
* @return Number of bands or a negative error code.
*/
int rtnl_qdisc_prio_get_bands(struct rtnl_qdisc *qdisc)
{
struct rtnl_prio *prio;
if (!(prio = rtnl_tc_data(TC_CAST(qdisc))))
BUG();
if (prio->qp_mask & SCH_PRIO_ATTR_BANDS)
return prio->qp_bands;
else
return -NLE_NOMEM;
}
/**
* Set priomap of the PRIO qdisc.
* @arg qdisc PRIO qdisc to be modified.
* @arg priomap New priority mapping.
* @arg len Length of priomap (# of elements).
* @return 0 on success or a negative error code.
*/
int rtnl_qdisc_prio_set_priomap(struct rtnl_qdisc *qdisc, uint8_t priomap[],
int len)
{
struct rtnl_prio *prio;
int i;
if (!(prio = rtnl_tc_data(TC_CAST(qdisc))))
BUG();
if (!(prio->qp_mask & SCH_PRIO_ATTR_BANDS))
return -NLE_MISSING_ATTR;
if ((len / sizeof(uint8_t)) > (TC_PRIO_MAX+1))
return -NLE_RANGE;
for (i = 0; i <= TC_PRIO_MAX; i++) {
if (priomap[i] > prio->qp_bands)
return -NLE_RANGE;
}
memcpy(prio->qp_priomap, priomap, len);
prio->qp_mask |= SCH_PRIO_ATTR_PRIOMAP;
return 0;
}
/**
* Get priomap of a PRIO qdisc.
* @arg qdisc PRIO qdisc.
* @return Priority mapping as array of size TC_PRIO_MAX+1
* or NULL if an error occured.
*/
uint8_t *rtnl_qdisc_prio_get_priomap(struct rtnl_qdisc *qdisc)
{
struct rtnl_prio *prio;
if (!(prio = rtnl_tc_data(TC_CAST(qdisc))))
BUG();
if (prio->qp_mask & SCH_PRIO_ATTR_PRIOMAP)
return prio->qp_priomap;
else
return NULL;
}
/** @} */
/**
* @name Priority Band Translations
* @{
*/
static const struct trans_tbl prios[] = {
__ADD(TC_PRIO_BESTEFFORT,besteffort),
__ADD(TC_PRIO_FILLER,filler),
__ADD(TC_PRIO_BULK,bulk),
__ADD(TC_PRIO_INTERACTIVE_BULK,interactive_bulk),
__ADD(TC_PRIO_INTERACTIVE,interactive),
__ADD(TC_PRIO_CONTROL,control),
};
/**
* Convert priority to character string.
* @arg prio Priority.
* @arg buf Destination buffer
* @arg size Size of destination buffer.
*
* Converts a priority to a character string and stores the result in
* the specified destination buffer.
*
* @return Name of priority as character string.
*/
char * rtnl_prio2str(int prio, char *buf, size_t size)
{
return __type2str(prio, buf, size, prios, ARRAY_SIZE(prios));
}
/**
* Convert character string to priority.
* @arg name Name of priority.
*
* Converts the provided character string specifying a priority
* to the corresponding numeric value.
*
* @return Numeric priority or a negative value if no match was found.
*/
int rtnl_str2prio(const char *name)
{
return __str2type(name, prios, ARRAY_SIZE(prios));
}
/** @} */
static struct rtnl_tc_ops prio_ops = {
.to_kind = "prio",
.to_type = RTNL_TC_TYPE_QDISC,
.to_size = sizeof(struct rtnl_prio),
.to_msg_parser = prio_msg_parser,
.to_dump = {
[NL_DUMP_LINE] = prio_dump_line,
[NL_DUMP_DETAILS] = prio_dump_details,
},
.to_msg_fill = prio_msg_fill,
};
static struct rtnl_tc_ops pfifo_fast_ops = {
.to_kind = "pfifo_fast",
.to_type = RTNL_TC_TYPE_QDISC,
.to_size = sizeof(struct rtnl_prio),
.to_msg_parser = prio_msg_parser,
.to_dump = {
[NL_DUMP_LINE] = prio_dump_line,
[NL_DUMP_DETAILS] = prio_dump_details,
},
.to_msg_fill = prio_msg_fill,
};
static void __init prio_init(void)
{
rtnl_tc_register(&prio_ops);
rtnl_tc_register(&pfifo_fast_ops);
}
static void __exit prio_exit(void)
{
rtnl_tc_unregister(&prio_ops);
rtnl_tc_unregister(&pfifo_fast_ops);
}
/** @} */
|