• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2003-2011 Thomas Graf <tgraf@suug.ch>
4  */
5 
6 /**
7  * @ingroup qdisc
8  * @ingroup class
9  * @defgroup qdisc_dsmark Differentiated Services Marker (DSMARK)
10  * @{
11  */
12 
13 #include <netlink-private/netlink.h>
14 #include <netlink-private/tc.h>
15 #include <netlink/netlink.h>
16 #include <netlink/utils.h>
17 #include <netlink/route/qdisc.h>
18 #include <netlink-private/route/tc-api.h>
19 #include <netlink/route/class.h>
20 #include <netlink/route/qdisc/dsmark.h>
21 
22 /** @cond SKIP */
23 #define SCH_DSMARK_ATTR_INDICES		0x1
24 #define SCH_DSMARK_ATTR_DEFAULT_INDEX	0x2
25 #define SCH_DSMARK_ATTR_SET_TC_INDEX	0x4
26 
27 #define SCH_DSMARK_ATTR_MASK		0x1
28 #define SCH_DSMARK_ATTR_VALUE		0x2
29 /** @endcond */
30 
31 static struct nla_policy dsmark_policy[TCA_DSMARK_MAX+1] = {
32 	[TCA_DSMARK_INDICES]		= { .type = NLA_U16 },
33 	[TCA_DSMARK_DEFAULT_INDEX]	= { .type = NLA_U16 },
34 	[TCA_DSMARK_SET_TC_INDEX]	= { .type = NLA_FLAG },
35 	[TCA_DSMARK_VALUE]		= { .type = NLA_U8 },
36 	[TCA_DSMARK_MASK]		= { .type = NLA_U8 },
37 };
38 
dsmark_qdisc_msg_parser(struct rtnl_tc * tc,void * data)39 static int dsmark_qdisc_msg_parser(struct rtnl_tc *tc, void *data)
40 {
41 	struct rtnl_dsmark_qdisc *dsmark = data;
42 	struct nlattr *tb[TCA_DSMARK_MAX + 1];
43 	int err;
44 
45 	err = tca_parse(tb, TCA_DSMARK_MAX, tc, dsmark_policy);
46 	if (err < 0)
47 		return err;
48 
49 	if (tb[TCA_DSMARK_INDICES]) {
50 		dsmark->qdm_indices = nla_get_u16(tb[TCA_DSMARK_INDICES]);
51 		dsmark->qdm_mask |= SCH_DSMARK_ATTR_INDICES;
52 	}
53 
54 	if (tb[TCA_DSMARK_DEFAULT_INDEX]) {
55 		dsmark->qdm_default_index =
56 				nla_get_u16(tb[TCA_DSMARK_DEFAULT_INDEX]);
57 		dsmark->qdm_mask |= SCH_DSMARK_ATTR_DEFAULT_INDEX;
58 	}
59 
60 	if (tb[TCA_DSMARK_SET_TC_INDEX]) {
61 		dsmark->qdm_set_tc_index = 1;
62 		dsmark->qdm_mask |= SCH_DSMARK_ATTR_SET_TC_INDEX;
63 	}
64 
65 	return 0;
66 }
67 
dsmark_class_msg_parser(struct rtnl_tc * tc,void * data)68 static int dsmark_class_msg_parser(struct rtnl_tc *tc, void *data)
69 {
70 	struct rtnl_dsmark_class *dsmark = data;
71 	struct nlattr *tb[TCA_DSMARK_MAX + 1];
72 	int err;
73 
74 	err = tca_parse(tb, TCA_DSMARK_MAX, tc, dsmark_policy);
75 	if (err < 0)
76 		return err;
77 
78 	if (tb[TCA_DSMARK_MASK]) {
79 		dsmark->cdm_bmask = nla_get_u8(tb[TCA_DSMARK_MASK]);
80 		dsmark->cdm_mask |= SCH_DSMARK_ATTR_MASK;
81 	}
82 
83 	if (tb[TCA_DSMARK_VALUE]) {
84 		dsmark->cdm_value = nla_get_u8(tb[TCA_DSMARK_VALUE]);
85 		dsmark->cdm_mask |= SCH_DSMARK_ATTR_VALUE;
86 	}
87 
88 	return 0;
89 }
90 
dsmark_qdisc_dump_line(struct rtnl_tc * tc,void * data,struct nl_dump_params * p)91 static void dsmark_qdisc_dump_line(struct rtnl_tc *tc, void *data,
92 				   struct nl_dump_params *p)
93 {
94 	struct rtnl_dsmark_qdisc *dsmark = data;
95 
96 	if (dsmark && (dsmark->qdm_mask & SCH_DSMARK_ATTR_INDICES))
97 		nl_dump(p, " indices 0x%04x", dsmark->qdm_indices);
98 }
99 
dsmark_qdisc_dump_details(struct rtnl_tc * tc,void * data,struct nl_dump_params * p)100 static void dsmark_qdisc_dump_details(struct rtnl_tc *tc, void *data,
101 				      struct nl_dump_params *p)
102 {
103 	struct rtnl_dsmark_qdisc *dsmark = data;
104 
105 	if (!dsmark)
106 		return;
107 
108 	if (dsmark->qdm_mask & SCH_DSMARK_ATTR_DEFAULT_INDEX)
109 		nl_dump(p, " default index 0x%04x", dsmark->qdm_default_index);
110 
111 	if (dsmark->qdm_mask & SCH_DSMARK_ATTR_SET_TC_INDEX)
112 		nl_dump(p, " set-tc-index");
113 }
114 
dsmark_class_dump_line(struct rtnl_tc * tc,void * data,struct nl_dump_params * p)115 static void dsmark_class_dump_line(struct rtnl_tc *tc, void *data,
116 				   struct nl_dump_params *p)
117 {
118 	struct rtnl_dsmark_class *dsmark = data;
119 
120 	if (!dsmark)
121 		return;
122 
123 	if (dsmark->cdm_mask & SCH_DSMARK_ATTR_VALUE)
124 		nl_dump(p, " value 0x%02x", dsmark->cdm_value);
125 
126 	if (dsmark->cdm_mask & SCH_DSMARK_ATTR_MASK)
127 		nl_dump(p, " mask 0x%02x", dsmark->cdm_bmask);
128 }
129 
dsmark_qdisc_msg_fill(struct rtnl_tc * tc,void * data,struct nl_msg * msg)130 static int dsmark_qdisc_msg_fill(struct rtnl_tc *tc, void *data,
131 				 struct nl_msg *msg)
132 {
133 	struct rtnl_dsmark_qdisc *dsmark = data;
134 
135 	if (!dsmark)
136 		return 0;
137 
138 	if (dsmark->qdm_mask & SCH_DSMARK_ATTR_INDICES)
139 		NLA_PUT_U16(msg, TCA_DSMARK_INDICES, dsmark->qdm_indices);
140 
141 	if (dsmark->qdm_mask & SCH_DSMARK_ATTR_DEFAULT_INDEX)
142 		NLA_PUT_U16(msg, TCA_DSMARK_DEFAULT_INDEX,
143 			    dsmark->qdm_default_index);
144 
145 	if (dsmark->qdm_mask & SCH_DSMARK_ATTR_SET_TC_INDEX)
146 		NLA_PUT_FLAG(msg, TCA_DSMARK_SET_TC_INDEX);
147 
148 	return 0;
149 
150 nla_put_failure:
151 	return -NLE_MSGSIZE;
152 }
153 
dsmark_class_msg_fill(struct rtnl_tc * tc,void * data,struct nl_msg * msg)154 static int dsmark_class_msg_fill(struct rtnl_tc *tc, void *data,
155 				 struct nl_msg *msg)
156 {
157 	struct rtnl_dsmark_class *dsmark = data;
158 
159 	if (!dsmark)
160 		return 0;
161 
162 	if (dsmark->cdm_mask & SCH_DSMARK_ATTR_MASK)
163 		NLA_PUT_U8(msg, TCA_DSMARK_MASK, dsmark->cdm_bmask);
164 
165 	if (dsmark->cdm_mask & SCH_DSMARK_ATTR_VALUE)
166 		NLA_PUT_U8(msg, TCA_DSMARK_VALUE, dsmark->cdm_value);
167 
168 	return 0;
169 
170 nla_put_failure:
171 	return -NLE_MSGSIZE;
172 }
173 
174 /**
175  * @name Class Attribute Access
176  * @{
177  */
178 
179 /**
180  * Set bitmask of DSMARK class.
181  * @arg class		DSMARK class to be modified.
182  * @arg mask		New bitmask.
183  * @return 0 on success or a negative error code.
184  */
rtnl_class_dsmark_set_bitmask(struct rtnl_class * class,uint8_t mask)185 int rtnl_class_dsmark_set_bitmask(struct rtnl_class *class, uint8_t mask)
186 {
187 	struct rtnl_dsmark_class *dsmark;
188 
189 	if (!(dsmark = rtnl_tc_data(TC_CAST(class))))
190 		return -NLE_NOMEM;
191 
192 	dsmark->cdm_bmask = mask;
193 	dsmark->cdm_mask |= SCH_DSMARK_ATTR_MASK;
194 
195 	return 0;
196 }
197 
198 /**
199  * Get bitmask of DSMARK class.
200  * @arg class		DSMARK class.
201  * @return Bitmask or a negative error code.
202  */
rtnl_class_dsmark_get_bitmask(struct rtnl_class * class)203 int rtnl_class_dsmark_get_bitmask(struct rtnl_class *class)
204 {
205 	struct rtnl_dsmark_class *dsmark;
206 
207 	if (!(dsmark = rtnl_tc_data(TC_CAST(class))))
208 		return -NLE_NOMEM;
209 
210 	if (dsmark->cdm_mask & SCH_DSMARK_ATTR_MASK)
211 		return dsmark->cdm_bmask;
212 	else
213 		return -NLE_NOATTR;
214 }
215 
216 /**
217  * Set value of DSMARK class.
218  * @arg class		DSMARK class to be modified.
219  * @arg value		New value.
220  * @return 0 on success or a negative errror code.
221  */
rtnl_class_dsmark_set_value(struct rtnl_class * class,uint8_t value)222 int rtnl_class_dsmark_set_value(struct rtnl_class *class, uint8_t value)
223 {
224 	struct rtnl_dsmark_class *dsmark;
225 
226 	if (!(dsmark = rtnl_tc_data(TC_CAST(class))))
227 		return -NLE_NOMEM;
228 
229 	dsmark->cdm_value = value;
230 	dsmark->cdm_mask |= SCH_DSMARK_ATTR_VALUE;
231 
232 	return 0;
233 }
234 
235 /**
236  * Get value of DSMARK class.
237  * @arg class		DSMARK class.
238  * @return Value or a negative error code.
239  */
rtnl_class_dsmark_get_value(struct rtnl_class * class)240 int rtnl_class_dsmark_get_value(struct rtnl_class *class)
241 {
242 	struct rtnl_dsmark_class *dsmark;
243 
244 	if (!(dsmark = rtnl_tc_data(TC_CAST(class))))
245 		return -NLE_NOMEM;
246 
247 	if (dsmark->cdm_mask & SCH_DSMARK_ATTR_VALUE)
248 		return dsmark->cdm_value;
249 	else
250 		return -NLE_NOATTR;
251 }
252 
253 /** @} */
254 
255 /**
256  * @name Qdisc Attribute Access
257  * @{
258  */
259 
260 /**
261  * Set indices of DSMARK qdisc.
262  * @arg qdisc		DSMARK qdisc to be modified.
263  * @arg indices		New indices.
264  */
rtnl_qdisc_dsmark_set_indices(struct rtnl_qdisc * qdisc,uint16_t indices)265 int rtnl_qdisc_dsmark_set_indices(struct rtnl_qdisc *qdisc, uint16_t indices)
266 {
267 	struct rtnl_dsmark_qdisc *dsmark;
268 
269 	if (!(dsmark = rtnl_tc_data(TC_CAST(qdisc))))
270 		return -NLE_NOMEM;
271 
272 	dsmark->qdm_indices = indices;
273 	dsmark->qdm_mask |= SCH_DSMARK_ATTR_INDICES;
274 
275 	return 0;
276 }
277 
278 /**
279  * Get indices of DSMARK qdisc.
280  * @arg qdisc		DSMARK qdisc.
281  * @return Indices or a negative error code.
282  */
rtnl_qdisc_dsmark_get_indices(struct rtnl_qdisc * qdisc)283 int rtnl_qdisc_dsmark_get_indices(struct rtnl_qdisc *qdisc)
284 {
285 	struct rtnl_dsmark_qdisc *dsmark;
286 
287 	if (!(dsmark = rtnl_tc_data(TC_CAST(qdisc))))
288 		return -NLE_NOMEM;
289 
290 	if (dsmark->qdm_mask & SCH_DSMARK_ATTR_INDICES)
291 		return dsmark->qdm_indices;
292 	else
293 		return -NLE_NOATTR;
294 }
295 
296 /**
297  * Set default index of DSMARK qdisc.
298  * @arg qdisc		DSMARK qdisc to be modified.
299  * @arg default_index	New default index.
300  * @return 0 on success or a negative error code.
301  */
rtnl_qdisc_dsmark_set_default_index(struct rtnl_qdisc * qdisc,uint16_t default_index)302 int rtnl_qdisc_dsmark_set_default_index(struct rtnl_qdisc *qdisc,
303 					uint16_t default_index)
304 {
305 	struct rtnl_dsmark_qdisc *dsmark;
306 
307 	if (!(dsmark = rtnl_tc_data(TC_CAST(qdisc))))
308 		return -NLE_NOMEM;
309 
310 	dsmark->qdm_default_index = default_index;
311 	dsmark->qdm_mask |= SCH_DSMARK_ATTR_DEFAULT_INDEX;
312 
313 	return 0;
314 }
315 
316 /**
317  * Get default index of DSMARK qdisc.
318  * @arg qdisc		DSMARK qdisc.
319  * @return Default index or a negative error code.
320  */
rtnl_qdisc_dsmark_get_default_index(struct rtnl_qdisc * qdisc)321 int rtnl_qdisc_dsmark_get_default_index(struct rtnl_qdisc *qdisc)
322 {
323 	struct rtnl_dsmark_qdisc *dsmark;
324 
325 	if (!(dsmark = rtnl_tc_data(TC_CAST(qdisc))))
326 		return -NLE_NOMEM;
327 
328 	if (dsmark->qdm_mask & SCH_DSMARK_ATTR_DEFAULT_INDEX)
329 		return dsmark->qdm_default_index;
330 	else
331 		return -NLE_NOATTR;
332 }
333 
334 /**
335  * Set set-tc-index flag of DSMARK qdisc.
336  * @arg qdisc		DSMARK qdisc to be modified.
337  * @arg flag		Flag indicating whether to enable or disable.
338  * @return 0 on success or a negative error code.
339  */
rtnl_qdisc_dsmark_set_set_tc_index(struct rtnl_qdisc * qdisc,int flag)340 int rtnl_qdisc_dsmark_set_set_tc_index(struct rtnl_qdisc *qdisc, int flag)
341 {
342 	struct rtnl_dsmark_qdisc *dsmark;
343 
344 	if (!(dsmark = rtnl_tc_data(TC_CAST(qdisc))))
345 		return -NLE_NOMEM;
346 
347 	dsmark->qdm_set_tc_index = !!flag;
348 	dsmark->qdm_mask |= SCH_DSMARK_ATTR_SET_TC_INDEX;
349 
350 	return 0;
351 }
352 
353 /**
354  * Get set-tc-index flag of DSMARK qdisc.
355  * @arg qdisc		DSMARK qdisc to be modified.
356  * @return 1 or 0 to indicate wehther the flag is enabled or a negative
357  *         error code.
358  */
rtnl_qdisc_dsmark_get_set_tc_index(struct rtnl_qdisc * qdisc)359 int rtnl_qdisc_dsmark_get_set_tc_index(struct rtnl_qdisc *qdisc)
360 {
361 	struct rtnl_dsmark_qdisc *dsmark;
362 
363 	if (!(dsmark = rtnl_tc_data(TC_CAST(qdisc))))
364 		return -NLE_NOMEM;
365 
366 	if (dsmark->qdm_mask & SCH_DSMARK_ATTR_SET_TC_INDEX)
367 		return dsmark->qdm_set_tc_index;
368 	else
369 		return -NLE_NOATTR;
370 }
371 
372 /** @} */
373 
374 static struct rtnl_tc_ops dsmark_qdisc_ops = {
375 	.to_kind		= "dsmark",
376 	.to_type		= RTNL_TC_TYPE_QDISC,
377 	.to_size		= sizeof(struct rtnl_dsmark_qdisc),
378 	.to_msg_parser		= dsmark_qdisc_msg_parser,
379 	.to_dump = {
380 	    [NL_DUMP_LINE]	= dsmark_qdisc_dump_line,
381 	    [NL_DUMP_DETAILS]	= dsmark_qdisc_dump_details,
382 	},
383 	.to_msg_fill		= dsmark_qdisc_msg_fill,
384 };
385 
386 static struct rtnl_tc_ops dsmark_class_ops = {
387 	.to_kind		= "dsmark",
388 	.to_type		= RTNL_TC_TYPE_CLASS,
389 	.to_size		= sizeof(struct rtnl_dsmark_class),
390 	.to_msg_parser		= dsmark_class_msg_parser,
391 	.to_dump[NL_DUMP_LINE]	= dsmark_class_dump_line,
392 	.to_msg_fill		= dsmark_class_msg_fill,
393 };
394 
dsmark_init(void)395 static void __init dsmark_init(void)
396 {
397 	rtnl_tc_register(&dsmark_qdisc_ops);
398 	rtnl_tc_register(&dsmark_class_ops);
399 }
400 
dsmark_exit(void)401 static void __exit dsmark_exit(void)
402 {
403 	rtnl_tc_unregister(&dsmark_qdisc_ops);
404 	rtnl_tc_unregister(&dsmark_class_ops);
405 }
406 
407 /** @} */
408