summaryrefslogtreecommitdiff
path: root/lib/route/qdisc/hfsc.c
blob: ddd124257a83873410d7c0521978f88edc79d2de (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
/*
 * lib/route/qdisc/hfsc.c	HFSC Qdisc
 *
 *	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) 2014 Cong Wang <xiyou.wangcong@gmail.com>
 */

/**
 * @ingroup qdisc
 * @ingroup class
 * @defgroup qdisc_hfsc Hierarchical Fair Service Curve (HFSC)
 * @{
 */

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

/** @cond SKIP */
#define SCH_HFSC_CLS_HAS_RSC		0x001
#define SCH_HFSC_CLS_HAS_FSC		0x002
#define SCH_HFSC_CLS_HAS_USC		0x004

#define SCH_HFSC_QD_HAS_DEFCLS		0x01
/** @endcond */

static struct nla_policy hfsc_policy[TCA_HFSC_MAX + 1] = {
	[TCA_HFSC_RSC]  = { .minlen = sizeof(struct tc_service_curve) },
	[TCA_HFSC_FSC]  = { .minlen = sizeof(struct tc_service_curve) },
	[TCA_HFSC_USC]  = { .minlen = sizeof(struct tc_service_curve) },
};

static int hfsc_qdisc_msg_parser(struct rtnl_tc *tc, void *data)
{
	struct rtnl_hfsc_qdisc *hfsc = data;
	struct tc_hfsc_qopt *opts;

	opts = (struct tc_hfsc_qopt *) tc->tc_opts->d_data;
	hfsc->qh_defcls = opts->defcls;
	hfsc->qh_mask |= SCH_HFSC_QD_HAS_DEFCLS;
	return 0;
}

static int hfsc_class_msg_parser(struct rtnl_tc *tc, void *data)
{
	struct nlattr *tb[TCA_HFSC_MAX + 1];
	struct rtnl_hfsc_class *hfsc = data;
	int err;

	if ((err = tca_parse(tb, TCA_HFSC_MAX, tc, hfsc_policy)) < 0)
		return err;

	if (tb[TCA_HFSC_RSC]) {
		struct tc_service_curve tsc;

		nla_memcpy(&tsc, tb[TCA_HFSC_RSC], sizeof(tsc));
		hfsc->ch_rsc = tsc;
		hfsc->ch_mask |= SCH_HFSC_CLS_HAS_RSC;
	}

	if (tb[TCA_HFSC_FSC]) {
		struct tc_service_curve tsc;

		nla_memcpy(&tsc, tb[TCA_HFSC_FSC], sizeof(tsc));
		hfsc->ch_fsc = tsc;
		hfsc->ch_mask |= SCH_HFSC_CLS_HAS_FSC;
	}

	if (tb[TCA_HFSC_USC]) {
		struct tc_service_curve tsc;

		nla_memcpy(&tsc, tb[TCA_HFSC_USC], sizeof(tsc));
		hfsc->ch_usc = tsc;
		hfsc->ch_mask |= SCH_HFSC_CLS_HAS_USC;
	}

	return 0;
}

static void hfsc_qdisc_dump_line(struct rtnl_tc *tc, void *data,
				struct nl_dump_params *p)
{
	struct rtnl_hfsc_qdisc *hfsc = data;

	if (!hfsc)
		return;

	if (hfsc->qh_mask & SCH_HFSC_QD_HAS_DEFCLS) {
		char buf[64];
		nl_dump(p, " default-class %s",
			rtnl_tc_handle2str(hfsc->qh_defcls, buf, sizeof(buf)));
	}
}

static void hfsc_dump_tsc(struct nl_dump_params *p, struct tc_service_curve *tsc)
{
	nl_dump(p, " m1 %u d %u m2 %u\n", tsc->m1, tsc->d, tsc->m2);
}

static void hfsc_class_dump_line(struct rtnl_tc *tc, void *data,
				struct nl_dump_params *p)
{
	struct rtnl_hfsc_class *hfsc = data;

	if (!hfsc)
		return;
	if (hfsc->ch_mask & SCH_HFSC_CLS_HAS_RSC)
		hfsc_dump_tsc(p, &hfsc->ch_rsc);
	if (hfsc->ch_mask & SCH_HFSC_CLS_HAS_FSC)
		hfsc_dump_tsc(p, &hfsc->ch_fsc);
	if (hfsc->ch_mask & SCH_HFSC_CLS_HAS_USC)
		hfsc_dump_tsc(p, &hfsc->ch_usc);
}

static void hfsc_class_dump_details(struct rtnl_tc *tc, void *data,
				   struct nl_dump_params *p)
{
	return;
}

static int hfsc_qdisc_msg_fill(struct rtnl_tc *tc, void *data,
			      struct nl_msg *msg)
{
	struct rtnl_hfsc_qdisc *hfsc = data;
	struct tc_hfsc_qopt opts = {0};

	if (!hfsc)
		BUG();

	opts.defcls = hfsc->qh_defcls;
	return nlmsg_append(msg, &opts, sizeof(opts), NL_DONTPAD);
}

static int hfsc_class_msg_fill(struct rtnl_tc *tc, void *data,
			      struct nl_msg *msg)
{
	struct rtnl_hfsc_class *hfsc = data;
	struct tc_service_curve tsc;

	if (!hfsc)
		BUG();

	if (hfsc->ch_mask & SCH_HFSC_CLS_HAS_RSC) {
		tsc = hfsc->ch_rsc;
		NLA_PUT(msg, TCA_HFSC_RSC, sizeof(tsc), &tsc);
	}

	if (hfsc->ch_mask & SCH_HFSC_CLS_HAS_FSC) {
		tsc = hfsc->ch_fsc;
		NLA_PUT(msg, TCA_HFSC_FSC, sizeof(tsc), &tsc);
	}

	if (hfsc->ch_mask & SCH_HFSC_CLS_HAS_USC) {
		tsc = hfsc->ch_usc;
		NLA_PUT(msg, TCA_HFSC_USC, sizeof(tsc), &tsc);
	}

	return 0;

nla_put_failure:
	return -NLE_MSGSIZE;
}

static struct rtnl_tc_ops hfsc_qdisc_ops;
static struct rtnl_tc_ops hfsc_class_ops;

static struct rtnl_hfsc_qdisc *hfsc_qdisc_data(const struct rtnl_qdisc *qdisc, int *err)
{
	return rtnl_tc_data_check(TC_CAST(qdisc), &hfsc_qdisc_ops, err);
}

static struct rtnl_hfsc_class *hfsc_class_data(const struct rtnl_class *class, int *err)
{
	return rtnl_tc_data_check(TC_CAST(class), &hfsc_class_ops, err);
}

/**
 * @name Attribute Modifications
 * @{
 */

/**
 * Return default class of HFSC qdisc
 * @arg qdisc		hfsc qdisc object
 *
 * Returns the classid of the class where all unclassified traffic
 * goes to.
 *
 * @return classid or TC_H_UNSPEC if unspecified.
 */
uint32_t rtnl_qdisc_hfsc_get_defcls(const struct rtnl_qdisc *qdisc)
{
	struct rtnl_hfsc_qdisc *hfsc;

	if ((hfsc = hfsc_qdisc_data(qdisc, NULL)) &&
	    (hfsc->qh_mask & SCH_HFSC_QD_HAS_DEFCLS))
		return hfsc->qh_defcls;

	return TC_H_UNSPEC;
}

/**
 * Set default class of the hfsc qdisc to the specified value
 * @arg qdisc		qdisc to change
 * @arg defcls		new default class
 */
int rtnl_qdisc_hfsc_set_defcls(struct rtnl_qdisc *qdisc, uint32_t defcls)
{
	struct rtnl_hfsc_qdisc *hfsc;
	int err;

	if (!(hfsc = hfsc_qdisc_data(qdisc, &err)))
		return err;

	hfsc->qh_defcls = defcls;
	hfsc->qh_mask |= SCH_HFSC_QD_HAS_DEFCLS;

	return 0;
}

int rtnl_class_hfsc_get_rsc(const struct rtnl_class *class, struct tc_service_curve *tsc)
{
	struct rtnl_hfsc_class *hfsc;
	int err = -NLE_OPNOTSUPP;

	if ((hfsc = hfsc_class_data(class, &err)) &&
	    (hfsc->ch_mask & SCH_HFSC_CLS_HAS_RSC)) {
		*tsc = hfsc->ch_rsc;
		return 0;
	}

	return err;
}

int rtnl_class_hfsc_set_rsc(struct rtnl_class *class, const struct tc_service_curve *tsc)
{
	struct rtnl_hfsc_class *hfsc;
	int err;

	if (!(hfsc = hfsc_class_data(class, &err)))
		return err;

	hfsc->ch_rsc = *tsc;
	hfsc->ch_mask |= SCH_HFSC_CLS_HAS_RSC;

	return 0;
}

int rtnl_class_hfsc_get_fsc(const struct rtnl_class *class, struct tc_service_curve *tsc)
{
	struct rtnl_hfsc_class *hfsc;
	int err = -NLE_OPNOTSUPP;

	if ((hfsc = hfsc_class_data(class, &err)) &&
	    (hfsc->ch_mask & SCH_HFSC_CLS_HAS_FSC)) {
		*tsc = hfsc->ch_fsc;
		return 0;
	}

	return err;
}

int rtnl_class_hfsc_set_fsc(struct rtnl_class *class, const struct tc_service_curve *tsc)
{
	struct rtnl_hfsc_class *hfsc;
	int err;

	if (!(hfsc = hfsc_class_data(class, &err)))
		return err;

	hfsc->ch_fsc = *tsc;
	hfsc->ch_mask |= SCH_HFSC_CLS_HAS_FSC;

	return 0;
}

int rtnl_class_hfsc_get_usc(const struct rtnl_class *class, struct tc_service_curve *tsc)
{
	struct rtnl_hfsc_class *hfsc;
	int err = -NLE_OPNOTSUPP;

	if ((hfsc = hfsc_class_data(class, &err)) &&
	    (hfsc->ch_mask & SCH_HFSC_CLS_HAS_USC)) {
		*tsc = hfsc->ch_usc;
		return 0;
	}

	return err;
}

int rtnl_class_hfsc_set_usc(struct rtnl_class *class, const struct tc_service_curve *tsc)
{
	struct rtnl_hfsc_class *hfsc;
	int err;

	if (!(hfsc = hfsc_class_data(class, &err)))
		return err;

	hfsc->ch_usc = *tsc;
	hfsc->ch_mask |= SCH_HFSC_CLS_HAS_USC;

	return 0;
}

/** @} */

static struct rtnl_tc_ops hfsc_qdisc_ops = {
	.to_kind		= "hfsc",
	.to_type		= RTNL_TC_TYPE_QDISC,
	.to_size		= sizeof(struct rtnl_hfsc_qdisc),
	.to_msg_parser		= hfsc_qdisc_msg_parser,
	.to_dump[NL_DUMP_LINE]	= hfsc_qdisc_dump_line,
	.to_msg_fill		= hfsc_qdisc_msg_fill,
};

static struct rtnl_tc_ops hfsc_class_ops = {
	.to_kind		= "hfsc",
	.to_type		= RTNL_TC_TYPE_CLASS,
	.to_size		= sizeof(struct rtnl_hfsc_class),
	.to_msg_parser		= hfsc_class_msg_parser,
	.to_dump = {
	    [NL_DUMP_LINE]	= hfsc_class_dump_line,
	    [NL_DUMP_DETAILS]	= hfsc_class_dump_details,
	},
	.to_msg_fill		= hfsc_class_msg_fill,
};

static void __init hfsc_init(void)
{
	rtnl_tc_register(&hfsc_qdisc_ops);
	rtnl_tc_register(&hfsc_class_ops);
}

static void __exit hfsc_exit(void)
{
	rtnl_tc_unregister(&hfsc_qdisc_ops);
	rtnl_tc_unregister(&hfsc_class_ops);
}

/** @} */