oops
[libnl.git] / lib / route / sch / dsmark.c
1 /*
2  * lib/route/sch/dsmark.c       DSMARK
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  */
11
12 /**
13  * @ingroup qdisc
14  * @ingroup class
15  * @defgroup dsmark Differentiated Services Marker (DSMARK)
16  * @{
17  */
18
19 #include <netlink-local.h>
20 #include <netlink-tc.h>
21 #include <netlink/netlink.h>
22 #include <netlink/utils.h>
23 #include <netlink/route/qdisc.h>
24 #include <netlink/route/qdisc-modules.h>
25 #include <netlink/route/class.h>
26 #include <netlink/route/class-modules.h>
27 #include <netlink/route/sch/dsmark.h>
28
29 /** @cond SKIP */
30 #define SCH_DSMARK_ATTR_INDICES         0x1
31 #define SCH_DSMARK_ATTR_DEFAULT_INDEX   0x2
32 #define SCH_DSMARK_ATTR_SET_TC_INDEX    0x4
33
34 #define SCH_DSMARK_ATTR_MASK            0x1
35 #define SCH_DSMARK_ATTR_VALUE           0x2
36 /** @endcond */
37
38 static inline struct rtnl_dsmark_qdisc *dsmark_qdisc(struct rtnl_qdisc *qdisc)
39 {
40         return (struct rtnl_dsmark_qdisc *) qdisc->q_subdata;
41 }
42
43 static inline struct rtnl_dsmark_qdisc *
44 dsmark_qdisc_alloc(struct rtnl_qdisc *qdisc)
45 {
46         if (!qdisc->q_subdata)
47                 qdisc->q_subdata = calloc(1, sizeof(struct rtnl_dsmark_qdisc));
48
49         return dsmark_qdisc(qdisc);
50 }
51
52 static struct nla_policy dsmark_policy[TCA_DSMARK_MAX+1] = {
53         [TCA_DSMARK_INDICES]            = { .type = NLA_U16 },
54         [TCA_DSMARK_DEFAULT_INDEX]      = { .type = NLA_U16 },
55         [TCA_DSMARK_SET_TC_INDEX]       = { .type = NLA_FLAG },
56         [TCA_DSMARK_VALUE]              = { .type = NLA_U8 },
57         [TCA_DSMARK_MASK]               = { .type = NLA_U8 },
58 };
59
60 static int dsmark_qdisc_msg_parser(struct rtnl_qdisc *qdisc)
61 {
62         int err;
63         struct nlattr *tb[TCA_DSMARK_MAX + 1];
64         struct rtnl_dsmark_qdisc *dsmark;
65
66         err = tca_parse(tb, TCA_DSMARK_MAX, (struct rtnl_tca *) qdisc,
67                         dsmark_policy);
68         if (err < 0)
69                 return err;
70
71         dsmark = dsmark_qdisc_alloc(qdisc);
72         if (!dsmark)
73                 return nl_errno(ENOMEM);
74
75         if (tb[TCA_DSMARK_INDICES]) {
76                 dsmark->qdm_indices = nla_get_u16(tb[TCA_DSMARK_INDICES]);
77                 dsmark->qdm_mask |= SCH_DSMARK_ATTR_INDICES;
78         }
79
80         if (tb[TCA_DSMARK_DEFAULT_INDEX]) {
81                 dsmark->qdm_default_index =
82                                 nla_get_u16(tb[TCA_DSMARK_DEFAULT_INDEX]);
83                 dsmark->qdm_mask |= SCH_DSMARK_ATTR_DEFAULT_INDEX;
84         }
85
86         if (tb[TCA_DSMARK_SET_TC_INDEX]) {
87                 dsmark->qdm_set_tc_index = 1;
88                 dsmark->qdm_mask |= SCH_DSMARK_ATTR_SET_TC_INDEX;
89         }
90
91         return 0;
92 }
93
94 static inline struct rtnl_dsmark_class *dsmark_class(struct rtnl_class *class)
95 {
96         return (struct rtnl_dsmark_class *) class->c_subdata;
97 }
98
99 static inline struct rtnl_dsmark_class *
100 dsmark_class_alloc(struct rtnl_class *class)
101 {
102         if (!class->c_subdata)
103                 class->c_subdata = calloc(1, sizeof(struct rtnl_dsmark_class));
104
105         return dsmark_class(class);
106 }
107
108 static int dsmark_class_msg_parser(struct rtnl_class *class)
109 {
110         int err;
111         struct nlattr *tb[TCA_DSMARK_MAX + 1];
112         struct rtnl_dsmark_class *dsmark;
113
114         err = tca_parse(tb, TCA_DSMARK_MAX, (struct rtnl_tca *) class,
115                         dsmark_policy);
116         if (err < 0)
117                 return err;
118
119         dsmark = dsmark_class_alloc(class);
120         if (!dsmark)
121                 return nl_errno(ENOMEM);
122
123         if (tb[TCA_DSMARK_MASK]) {
124                 dsmark->cdm_bmask = nla_get_u8(tb[TCA_DSMARK_MASK]);
125                 dsmark->cdm_mask |= SCH_DSMARK_ATTR_MASK;
126         }
127
128         if (tb[TCA_DSMARK_VALUE]) {
129                 dsmark->cdm_value = nla_get_u8(tb[TCA_DSMARK_VALUE]);
130                 dsmark->cdm_mask |= SCH_DSMARK_ATTR_VALUE;
131         }
132
133         return 0;
134 }
135
136 static int dsmark_qdisc_dump_brief(struct rtnl_qdisc *qdisc,
137                                    struct nl_dump_params *p, int line)
138 {
139         struct rtnl_dsmark_qdisc *dsmark = dsmark_qdisc(qdisc);
140
141         if (dsmark && (dsmark->qdm_mask & SCH_DSMARK_ATTR_INDICES))
142                 dp_dump(p, " indices 0x%04x", dsmark->qdm_indices);
143
144         return line;
145 }
146
147 static int dsmark_qdisc_dump_full(struct rtnl_qdisc *qdisc,
148                                   struct nl_dump_params *p, int line)
149 {
150         struct rtnl_dsmark_qdisc *dsmark = dsmark_qdisc(qdisc);
151
152         if (!dsmark)
153                 goto ignore;
154
155         if (dsmark->qdm_mask & SCH_DSMARK_ATTR_DEFAULT_INDEX)
156                 dp_dump(p, " default index 0x%04x", dsmark->qdm_default_index);
157
158         if (dsmark->qdm_mask & SCH_DSMARK_ATTR_SET_TC_INDEX)
159                 dp_dump(p, " set-tc-index");
160
161 ignore:
162         return line;
163 }
164
165 static int dsmark_class_dump_brief(struct rtnl_class *class,
166                                    struct nl_dump_params *p, int line)
167 {
168         struct rtnl_dsmark_class *dsmark = dsmark_class(class);
169
170         if (!dsmark)
171                 goto ignore;
172
173         if (dsmark->cdm_mask & SCH_DSMARK_ATTR_VALUE)
174                 dp_dump(p, " value 0x%02x", dsmark->cdm_value);
175
176         if (dsmark->cdm_mask & SCH_DSMARK_ATTR_MASK)
177                 dp_dump(p, " mask 0x%02x", dsmark->cdm_bmask);
178
179 ignore:
180         return line;
181 }
182
183 static struct nl_msg *dsmark_qdisc_get_opts(struct rtnl_qdisc *qdisc)
184 {
185         struct rtnl_dsmark_qdisc *dsmark = dsmark_qdisc(qdisc);
186         struct nl_msg *msg;
187
188         if (!dsmark)
189                 return NULL;
190
191         msg = nlmsg_build_no_hdr();
192         if (!msg)
193                 goto nla_put_failure;
194
195         if (dsmark->qdm_mask & SCH_DSMARK_ATTR_INDICES)
196                 NLA_PUT_U16(msg, TCA_DSMARK_INDICES, dsmark->qdm_indices);
197
198         if (dsmark->qdm_mask & SCH_DSMARK_ATTR_DEFAULT_INDEX)
199                 NLA_PUT_U16(msg, TCA_DSMARK_DEFAULT_INDEX,
200                             dsmark->qdm_default_index);
201
202         if (dsmark->qdm_mask & SCH_DSMARK_ATTR_SET_TC_INDEX)
203                 NLA_PUT_FLAG(msg, TCA_DSMARK_SET_TC_INDEX);
204
205         return msg;
206
207 nla_put_failure:
208         nlmsg_free(msg);
209         return NULL;
210 }
211
212 static struct nl_msg *dsmark_class_get_opts(struct rtnl_class *class)
213 {
214         struct rtnl_dsmark_class *dsmark = dsmark_class(class);
215         struct nl_msg *msg;
216
217         if (!dsmark)
218                 return NULL;
219
220         msg = nlmsg_build_no_hdr();
221         if (!msg)
222                 goto nla_put_failure;
223
224         if (dsmark->cdm_mask & SCH_DSMARK_ATTR_MASK)
225                 NLA_PUT_U8(msg, TCA_DSMARK_MASK, dsmark->cdm_bmask);
226
227         if (dsmark->cdm_mask & SCH_DSMARK_ATTR_VALUE)
228                 NLA_PUT_U8(msg, TCA_DSMARK_VALUE, dsmark->cdm_value);
229
230         return msg;
231
232 nla_put_failure:
233         nlmsg_free(msg);
234         return NULL;
235 }
236
237 /**
238  * @name Class Attribute Access
239  * @{
240  */
241
242 /**
243  * Set bitmask of DSMARK class.
244  * @arg class           DSMARK class to be modified.
245  * @arg mask            New bitmask.
246  * @return 0 on success or a negative error code.
247  */
248 int rtnl_class_dsmark_set_bitmask(struct rtnl_class *class, uint8_t mask)
249 {
250         struct rtnl_dsmark_class *dsmark;
251         
252         dsmark = dsmark_class(class);
253         if (!dsmark)
254                 return nl_errno(ENOMEM);
255
256         dsmark->cdm_bmask = mask;
257         dsmark->cdm_mask |= SCH_DSMARK_ATTR_MASK;
258
259         return 0;
260 }
261
262 /**
263  * Get bitmask of DSMARK class.
264  * @arg class           DSMARK class.
265  * @return Bitmask or a negative error code.
266  */
267 int rtnl_class_dsmark_get_bitmask(struct rtnl_class *class)
268 {
269         struct rtnl_dsmark_class *dsmark;
270
271         dsmark = dsmark_class(class);
272         if (dsmark && dsmark->cdm_mask & SCH_DSMARK_ATTR_MASK)
273                 return dsmark->cdm_bmask;
274         else
275                 return nl_errno(ENOENT);
276 }
277
278 /**
279  * Set value of DSMARK class.
280  * @arg class           DSMARK class to be modified.
281  * @arg value           New value.
282  * @return 0 on success or a negative errror code.
283  */
284 int rtnl_class_dsmark_set_value(struct rtnl_class *class, uint8_t value)
285 {
286         struct rtnl_dsmark_class *dsmark;
287
288         dsmark = dsmark_class(class);
289         if (!dsmark)
290                 return nl_errno(ENOMEM);
291
292         dsmark->cdm_value = value;
293         dsmark->cdm_mask |= SCH_DSMARK_ATTR_VALUE;
294
295         return 0;
296 }
297
298 /**
299  * Get value of DSMARK class.
300  * @arg class           DSMARK class.
301  * @return Value or a negative error code.
302  */
303 int rtnl_class_dsmark_get_value(struct rtnl_class *class)
304 {
305         struct rtnl_dsmark_class *dsmark;
306
307         dsmark = dsmark_class(class);
308         if (dsmark && dsmark->cdm_mask & SCH_DSMARK_ATTR_VALUE)
309                 return dsmark->cdm_value;
310         else
311                 return nl_errno(ENOENT);
312 }
313
314 /** @} */
315
316 /**
317  * @name Qdisc Attribute Access
318  * @{
319  */
320
321 /**
322  * Set indices of DSMARK qdisc.
323  * @arg qdisc           DSMARK qdisc to be modified.
324  * @arg indices         New indices.
325  */
326 int rtnl_qdisc_dsmark_set_indices(struct rtnl_qdisc *qdisc, uint16_t indices)
327 {
328         struct rtnl_dsmark_qdisc *dsmark;
329
330         dsmark = dsmark_qdisc(qdisc);
331         if (!dsmark)
332                 return nl_errno(ENOMEM);
333
334         dsmark->qdm_indices = indices;
335         dsmark->qdm_mask |= SCH_DSMARK_ATTR_INDICES;
336
337         return 0;
338 }
339
340 /**
341  * Get indices of DSMARK qdisc.
342  * @arg qdisc           DSMARK qdisc.
343  * @return Indices or a negative error code.
344  */
345 int rtnl_qdisc_dsmark_get_indices(struct rtnl_qdisc *qdisc)
346 {
347         struct rtnl_dsmark_qdisc *dsmark;
348
349         dsmark = dsmark_qdisc(qdisc);
350         if (dsmark && dsmark->qdm_mask & SCH_DSMARK_ATTR_INDICES)
351                 return dsmark->qdm_indices;
352         else
353                 return nl_errno(ENOENT);
354 }
355
356 /**
357  * Set default index of DSMARK qdisc.
358  * @arg qdisc           DSMARK qdisc to be modified.
359  * @arg default_index   New default index.
360  * @return 0 on success or a negative error code.
361  */
362 int rtnl_qdisc_dsmark_set_default_index(struct rtnl_qdisc *qdisc,
363                                         uint16_t default_index)
364 {
365         struct rtnl_dsmark_qdisc *dsmark;
366
367         dsmark = dsmark_qdisc(qdisc);
368         if (!dsmark)
369                 return nl_errno(ENOMEM);
370
371         dsmark->qdm_default_index = default_index;
372         dsmark->qdm_mask |= SCH_DSMARK_ATTR_DEFAULT_INDEX;
373
374         return 0;
375 }
376
377 /**
378  * Get default index of DSMARK qdisc.
379  * @arg qdisc           DSMARK qdisc.
380  * @return Default index or a negative error code.
381  */
382 int rtnl_qdisc_dsmark_get_default_index(struct rtnl_qdisc *qdisc)
383 {
384         struct rtnl_dsmark_qdisc *dsmark;
385
386         dsmark = dsmark_qdisc(qdisc);
387         if (dsmark && dsmark->qdm_mask & SCH_DSMARK_ATTR_DEFAULT_INDEX)
388                 return dsmark->qdm_default_index;
389         else
390                 return nl_errno(ENOENT);
391 }
392
393 /**
394  * Set set-tc-index flag of DSMARK qdisc.
395  * @arg qdisc           DSMARK qdisc to be modified.
396  * @arg flag            Flag indicating whether to enable or disable.
397  * @return 0 on success or a negative error code.
398  */
399 int rtnl_qdisc_dsmark_set_set_tc_index(struct rtnl_qdisc *qdisc, int flag)
400 {
401         struct rtnl_dsmark_qdisc *dsmark;
402
403         dsmark = dsmark_qdisc(qdisc);
404         if (!dsmark)
405                 return nl_errno(ENOMEM);
406
407         dsmark->qdm_set_tc_index = !!flag;
408         dsmark->qdm_mask |= SCH_DSMARK_ATTR_SET_TC_INDEX;
409
410         return 0;
411 }
412
413 /**
414  * Get set-tc-index flag of DSMARK qdisc.
415  * @arg qdisc           DSMARK qdisc to be modified.
416  * @return 1 or 0 to indicate wehther the flag is enabled or a negative
417  *         error code.
418  */
419 int rtnl_qdisc_dsmark_get_set_tc_index(struct rtnl_qdisc *qdisc)
420 {
421         struct rtnl_dsmark_qdisc *dsmark;
422
423         dsmark = dsmark_qdisc(qdisc);
424         if (dsmark && dsmark->qdm_mask & SCH_DSMARK_ATTR_SET_TC_INDEX)
425                 return dsmark->qdm_set_tc_index;
426         else
427                 return nl_errno(ENOENT);
428 }
429
430 /** @} */
431
432 static struct rtnl_qdisc_ops dsmark_qdisc_ops = {
433         .qo_kind                = "dsmark",
434         .qo_msg_parser          = dsmark_qdisc_msg_parser,
435         .qo_dump[NL_DUMP_BRIEF] = dsmark_qdisc_dump_brief,
436         .qo_dump[NL_DUMP_FULL]  = dsmark_qdisc_dump_full,
437         .qo_get_opts            = dsmark_qdisc_get_opts,
438 };
439
440 static struct rtnl_class_ops dsmark_class_ops = {
441         .co_kind                = "dsmark",
442         .co_msg_parser          = dsmark_class_msg_parser,
443         .co_dump[NL_DUMP_BRIEF] = dsmark_class_dump_brief,
444         .co_get_opts            = dsmark_class_get_opts,
445 };
446
447 static void __init dsmark_init(void)
448 {
449         rtnl_qdisc_register(&dsmark_qdisc_ops);
450         rtnl_class_register(&dsmark_class_ops);
451 }
452
453 static void __exit dsmark_exit(void)
454 {
455         rtnl_qdisc_unregister(&dsmark_qdisc_ops);
456         rtnl_class_unregister(&dsmark_class_ops);
457 }
458
459 /** @} */