summaryrefslogtreecommitdiffstats
path: root/lib/route/qdisc/dsmark.c
blob: fd9553dc84ee1749d8bc171b00a9b2e691591b7c (plain)
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
 * lib/route/qdisc/dsmark.c	DSMARK
 *
 *	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
 * @ingroup class
 * @defgroup qdisc_dsmark Differentiated Services Marker (DSMARK)
 * @{
 */

#include <netlink-private/netlink.h>
#include <netlink-private/tc.h>
#include <netlink/netlink.h>
#include <netlink/utils.h>
#include <netlink/route/qdisc.h>
#include <netlink-private/route/tc-api.h>
#include <netlink/route/class.h>
#include <netlink/route/qdisc/dsmark.h>

/** @cond SKIP */
#define SCH_DSMARK_ATTR_INDICES		0x1
#define SCH_DSMARK_ATTR_DEFAULT_INDEX	0x2
#define SCH_DSMARK_ATTR_SET_TC_INDEX	0x4

#define SCH_DSMARK_ATTR_MASK		0x1
#define SCH_DSMARK_ATTR_VALUE		0x2
/** @endcond */

static struct nla_policy dsmark_policy[TCA_DSMARK_MAX+1] = {
	[TCA_DSMARK_INDICES]		= { .type = NLA_U16 },
	[TCA_DSMARK_DEFAULT_INDEX]	= { .type = NLA_U16 },
	[TCA_DSMARK_SET_TC_INDEX]	= { .type = NLA_FLAG },
	[TCA_DSMARK_VALUE]		= { .type = NLA_U8 },
	[TCA_DSMARK_MASK]		= { .type = NLA_U8 },
};

static int dsmark_qdisc_msg_parser(struct rtnl_tc *tc, void *data)
{
	struct rtnl_dsmark_qdisc *dsmark = data;
	struct nlattr *tb[TCA_DSMARK_MAX + 1];
	int err;

	err = tca_parse(tb, TCA_DSMARK_MAX, tc, dsmark_policy);
	if (err < 0)
		return err;

	if (tb[TCA_DSMARK_INDICES]) {
		dsmark->qdm_indices = nla_get_u16(tb[TCA_DSMARK_INDICES]);
		dsmark->qdm_mask |= SCH_DSMARK_ATTR_INDICES;
	}

	if (tb[TCA_DSMARK_DEFAULT_INDEX]) {
		dsmark->qdm_default_index =
				nla_get_u16(tb[TCA_DSMARK_DEFAULT_INDEX]);
		dsmark->qdm_mask |= SCH_DSMARK_ATTR_DEFAULT_INDEX;
	}

	if (tb[TCA_DSMARK_SET_TC_INDEX]) {
		dsmark->qdm_set_tc_index = 1;
		dsmark->qdm_mask |= SCH_DSMARK_ATTR_SET_TC_INDEX;
	}

	return 0;
}

static int dsmark_class_msg_parser(struct rtnl_tc *tc, void *data)
{
	struct rtnl_dsmark_class *dsmark = data;
	struct nlattr *tb[TCA_DSMARK_MAX + 1];
	int err;

	err = tca_parse(tb, TCA_DSMARK_MAX, tc, dsmark_policy);
	if (err < 0)
		return err;

	if (tb[TCA_DSMARK_MASK]) {
		dsmark->cdm_bmask = nla_get_u8(tb[TCA_DSMARK_MASK]);
		dsmark->cdm_mask |= SCH_DSMARK_ATTR_MASK;
	}

	if (tb[TCA_DSMARK_VALUE]) {
		dsmark->cdm_value = nla_get_u8(tb[TCA_DSMARK_VALUE]);
		dsmark->cdm_mask |= SCH_DSMARK_ATTR_VALUE;
	}

	return 0;
}

static void dsmark_qdisc_dump_line(struct rtnl_tc *tc, void *data,
				   struct nl_dump_params *p)
{
	struct rtnl_dsmark_qdisc *dsmark = data;

	if (dsmark && (dsmark->qdm_mask & SCH_DSMARK_ATTR_INDICES))
		nl_dump(p, " indices 0x%04x", dsmark->qdm_indices);
}

static void dsmark_qdisc_dump_details(struct rtnl_tc *tc, void *data,
				      struct nl_dump_params *p)
{
	struct rtnl_dsmark_qdisc *dsmark = data;

	if (!dsmark)
		return;

	if (dsmark->qdm_mask & SCH_DSMARK_ATTR_DEFAULT_INDEX)
		nl_dump(p, " default index 0x%04x", dsmark->qdm_default_index);

	if (dsmark->qdm_mask & SCH_DSMARK_ATTR_SET_TC_INDEX)
		nl_dump(p, " set-tc-index");
}

static void dsmark_class_dump_line(struct rtnl_tc *tc, void *data,
				   struct nl_dump_params *p)
{
	struct rtnl_dsmark_class *dsmark = data;

	if (!dsmark)
		return;

	if (dsmark->cdm_mask & SCH_DSMARK_ATTR_VALUE)
		nl_dump(p, " value 0x%02x", dsmark->cdm_value);

	if (dsmark->cdm_mask & SCH_DSMARK_ATTR_MASK)
		nl_dump(p, " mask 0x%02x", dsmark->cdm_bmask);
}

static int dsmark_qdisc_msg_fill(struct rtnl_tc *tc, void *data,
				 struct nl_msg *msg)
{
	struct rtnl_dsmark_qdisc *dsmark = data;

	if (!dsmark)
		return 0;

	if (dsmark->qdm_mask & SCH_DSMARK_ATTR_INDICES)
		NLA_PUT_U16(msg, TCA_DSMARK_INDICES, dsmark->qdm_indices);

	if (dsmark->qdm_mask & SCH_DSMARK_ATTR_DEFAULT_INDEX)
		NLA_PUT_U16(msg, TCA_DSMARK_DEFAULT_INDEX,
			    dsmark->qdm_default_index);

	if (dsmark->qdm_mask & SCH_DSMARK_ATTR_SET_TC_INDEX)
		NLA_PUT_FLAG(msg, TCA_DSMARK_SET_TC_INDEX);

	return 0;

nla_put_failure:
	return -NLE_MSGSIZE;
}

static int dsmark_class_msg_fill(struct rtnl_tc *tc, void *data,
				 struct nl_msg *msg)
{
	struct rtnl_dsmark_class *dsmark = data;

	if (!dsmark)
		return 0;

	if (dsmark->cdm_mask & SCH_DSMARK_ATTR_MASK)
		NLA_PUT_U8(msg, TCA_DSMARK_MASK, dsmark->cdm_bmask);

	if (dsmark->cdm_mask & SCH_DSMARK_ATTR_VALUE)
		NLA_PUT_U8(msg, TCA_DSMARK_VALUE, dsmark->cdm_value);

	return 0;

nla_put_failure:
	return -NLE_MSGSIZE;
}

/**
 * @name Class Attribute Access
 * @{
 */

/**
 * Set bitmask of DSMARK class.
 * @arg class		DSMARK class to be modified.
 * @arg mask		New bitmask.
 * @return 0 on success or a negative error code.
 */
int rtnl_class_dsmark_set_bitmask(struct rtnl_class *class, uint8_t mask)
{
	struct rtnl_dsmark_class *dsmark;
	
	if (!(dsmark = rtnl_tc_data(TC_CAST(class))))
		return -NLE_NOMEM;

	dsmark->cdm_bmask = mask;
	dsmark->cdm_mask |= SCH_DSMARK_ATTR_MASK;

	return 0;
}

/**
 * Get bitmask of DSMARK class.
 * @arg class		DSMARK class.
 * @return Bitmask or a negative error code.
 */
int rtnl_class_dsmark_get_bitmask(struct rtnl_class *class)
{
	struct rtnl_dsmark_class *dsmark;
	
	if (!(dsmark = rtnl_tc_data(TC_CAST(class))))
		return -NLE_NOMEM;

	if (dsmark->cdm_mask & SCH_DSMARK_ATTR_MASK)
		return dsmark->cdm_bmask;
	else
		return -NLE_NOATTR;
}

/**
 * Set value of DSMARK class.
 * @arg class		DSMARK class to be modified.
 * @arg value		New value.
 * @return 0 on success or a negative errror code.
 */
int rtnl_class_dsmark_set_value(struct rtnl_class *class, uint8_t value)
{
	struct rtnl_dsmark_class *dsmark;
	
	if (!(dsmark = rtnl_tc_data(TC_CAST(class))))
		return -NLE_NOMEM;

	dsmark->cdm_value = value;
	dsmark->cdm_mask |= SCH_DSMARK_ATTR_VALUE;

	return 0;
}

/**
 * Get value of DSMARK class.
 * @arg class		DSMARK class.
 * @return Value or a negative error code.
 */
int rtnl_class_dsmark_get_value(struct rtnl_class *class)
{
	struct rtnl_dsmark_class *dsmark;
	
	if (!(dsmark = rtnl_tc_data(TC_CAST(class))))
		return -NLE_NOMEM;

	if (dsmark->cdm_mask & SCH_DSMARK_ATTR_VALUE)
		return dsmark->cdm_value;
	else
		return -NLE_NOATTR;
}

/** @} */

/**
 * @name Qdisc Attribute Access
 * @{
 */

/**
 * Set indices of DSMARK qdisc.
 * @arg qdisc		DSMARK qdisc to be modified.
 * @arg indices		New indices.
 */
int rtnl_qdisc_dsmark_set_indices(struct rtnl_qdisc *qdisc, uint16_t indices)
{
	struct rtnl_dsmark_qdisc *dsmark;

	if (!(dsmark = rtnl_tc_data(TC_CAST(qdisc))))
		return -NLE_NOMEM;

	dsmark->qdm_indices = indices;
	dsmark->qdm_mask |= SCH_DSMARK_ATTR_INDICES;

	return 0;
}

/**
 * Get indices of DSMARK qdisc.
 * @arg qdisc		DSMARK qdisc.
 * @return Indices or a negative error code.
 */
int rtnl_qdisc_dsmark_get_indices(struct rtnl_qdisc *qdisc)
{
	struct rtnl_dsmark_qdisc *dsmark;

	if (!(dsmark = rtnl_tc_data(TC_CAST(qdisc))))
		return -NLE_NOMEM;

	if (dsmark->qdm_mask & SCH_DSMARK_ATTR_INDICES)
		return dsmark->qdm_indices;
	else
		return -NLE_NOATTR;
}

/**
 * Set default index of DSMARK qdisc.
 * @arg qdisc		DSMARK qdisc to be modified.
 * @arg default_index	New default index.
 * @return 0 on success or a negative error code.
 */
int rtnl_qdisc_dsmark_set_default_index(struct rtnl_qdisc *qdisc,
					uint16_t default_index)
{
	struct rtnl_dsmark_qdisc *dsmark;

	if (!(dsmark = rtnl_tc_data(TC_CAST(qdisc))))
		return -NLE_NOMEM;

	dsmark->qdm_default_index = default_index;
	dsmark->qdm_mask |= SCH_DSMARK_ATTR_DEFAULT_INDEX;

	return 0;
}

/**
 * Get default index of DSMARK qdisc.
 * @arg qdisc		DSMARK qdisc.
 * @return Default index or a negative error code.
 */
int rtnl_qdisc_dsmark_get_default_index(struct rtnl_qdisc *qdisc)
{
	struct rtnl_dsmark_qdisc *dsmark;

	if (!(dsmark = rtnl_tc_data(TC_CAST(qdisc))))
		return -NLE_NOMEM;

	if (dsmark->qdm_mask & SCH_DSMARK_ATTR_DEFAULT_INDEX)
		return dsmark->qdm_default_index;
	else
		return -NLE_NOATTR;
}

/**
 * Set set-tc-index flag of DSMARK qdisc.
 * @arg qdisc		DSMARK qdisc to be modified.
 * @arg flag		Flag indicating whether to enable or disable.
 * @return 0 on success or a negative error code.
 */
int rtnl_qdisc_dsmark_set_set_tc_index(struct rtnl_qdisc *qdisc, int flag)
{
	struct rtnl_dsmark_qdisc *dsmark;

	if (!(dsmark = rtnl_tc_data(TC_CAST(qdisc))))
		return -NLE_NOMEM;

	dsmark->qdm_set_tc_index = !!flag;
	dsmark->qdm_mask |= SCH_DSMARK_ATTR_SET_TC_INDEX;

	return 0;
}

/**
 * Get set-tc-index flag of DSMARK qdisc.
 * @arg qdisc		DSMARK qdisc to be modified.
 * @return 1 or 0 to indicate wehther the flag is enabled or a negative
 *         error code.
 */
int rtnl_qdisc_dsmark_get_set_tc_index(struct rtnl_qdisc *qdisc)
{
	struct rtnl_dsmark_qdisc *dsmark;

	if (!(dsmark = rtnl_tc_data(TC_CAST(qdisc))))
		return -NLE_NOMEM;

	if (dsmark->qdm_mask & SCH_DSMARK_ATTR_SET_TC_INDEX)
		return dsmark->qdm_set_tc_index;
	else
		return -NLE_NOATTR;
}

/** @} */

static struct rtnl_tc_ops dsmark_qdisc_ops = {
	.to_kind		= "dsmark",
	.to_type		= RTNL_TC_TYPE_QDISC,
	.to_size		= sizeof(struct rtnl_dsmark_qdisc),
	.to_msg_parser		= dsmark_qdisc_msg_parser,
	.to_dump = {
	    [NL_DUMP_LINE]	= dsmark_qdisc_dump_line,
	    [NL_DUMP_DETAILS]	= dsmark_qdisc_dump_details,
	},
	.to_msg_fill		= dsmark_qdisc_msg_fill,
};

static struct rtnl_tc_ops dsmark_class_ops = {
	.to_kind		= "dsmark",
	.to_type		= RTNL_TC_TYPE_CLASS,
	.to_size		= sizeof(struct rtnl_dsmark_class),
	.to_msg_parser		= dsmark_class_msg_parser,
	.to_dump[NL_DUMP_LINE]	= dsmark_class_dump_line,
	.to_msg_fill		= dsmark_class_msg_fill,
};

static void __init dsmark_init(void)
{
	rtnl_tc_register(&dsmark_qdisc_ops);
	rtnl_tc_register(&dsmark_class_ops);
}

static void __exit dsmark_exit(void)
{
	rtnl_tc_unregister(&dsmark_qdisc_ops);
	rtnl_tc_unregister(&dsmark_class_ops);
}

/** @} */