sch_drr.c 11.5 KB
Newer Older
P
Patrick McHardy 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * net/sched/sch_drr.c         Deficit Round Robin scheduler
 *
 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 */

#include <linux/module.h>
12
#include <linux/slab.h>
P
Patrick McHardy 已提交
13 14 15 16 17 18 19 20 21 22 23 24
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/netdevice.h>
#include <linux/pkt_sched.h>
#include <net/sch_generic.h>
#include <net/pkt_sched.h>
#include <net/pkt_cls.h>

struct drr_class {
	struct Qdisc_class_common	common;
	unsigned int			filter_cnt;

25
	struct gnet_stats_basic_packed		bstats;
P
Patrick McHardy 已提交
26
	struct gnet_stats_queue		qstats;
27
	struct net_rate_estimator __rcu *rate_est;
P
Patrick McHardy 已提交
28 29 30 31 32 33 34 35 36
	struct list_head		alist;
	struct Qdisc			*qdisc;

	u32				quantum;
	u32				deficit;
};

struct drr_sched {
	struct list_head		active;
J
John Fastabend 已提交
37
	struct tcf_proto __rcu		*filter_list;
38
	struct tcf_block		*block;
P
Patrick McHardy 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
	struct Qdisc_class_hash		clhash;
};

static struct drr_class *drr_find_class(struct Qdisc *sch, u32 classid)
{
	struct drr_sched *q = qdisc_priv(sch);
	struct Qdisc_class_common *clc;

	clc = qdisc_class_find(&q->clhash, classid);
	if (clc == NULL)
		return NULL;
	return container_of(clc, struct drr_class, common);
}

static void drr_purge_queue(struct drr_class *cl)
{
	unsigned int len = cl->qdisc->q.qlen;
56
	unsigned int backlog = cl->qdisc->qstats.backlog;
P
Patrick McHardy 已提交
57 58

	qdisc_reset(cl->qdisc);
59
	qdisc_tree_reduce_backlog(cl->qdisc, len, backlog);
P
Patrick McHardy 已提交
60 61 62 63 64 65 66
}

static const struct nla_policy drr_policy[TCA_DRR_MAX + 1] = {
	[TCA_DRR_QUANTUM]	= { .type = NLA_U32 },
};

static int drr_change_class(struct Qdisc *sch, u32 classid, u32 parentid,
67 68
			    struct nlattr **tca, unsigned long *arg,
			    struct netlink_ext_ack *extack)
P
Patrick McHardy 已提交
69 70 71
{
	struct drr_sched *q = qdisc_priv(sch);
	struct drr_class *cl = (struct drr_class *)*arg;
72
	struct nlattr *opt = tca[TCA_OPTIONS];
P
Patrick McHardy 已提交
73 74 75 76
	struct nlattr *tb[TCA_DRR_MAX + 1];
	u32 quantum;
	int err;

77 78
	if (!opt) {
		NL_SET_ERR_MSG(extack, "DRR options are required for this operation");
79
		return -EINVAL;
80
	}
81

82
	err = nla_parse_nested(tb, TCA_DRR_MAX, opt, drr_policy, extack);
P
Patrick McHardy 已提交
83 84 85 86 87
	if (err < 0)
		return err;

	if (tb[TCA_DRR_QUANTUM]) {
		quantum = nla_get_u32(tb[TCA_DRR_QUANTUM]);
88 89
		if (quantum == 0) {
			NL_SET_ERR_MSG(extack, "Specified DRR quantum cannot be zero");
P
Patrick McHardy 已提交
90
			return -EINVAL;
91
		}
P
Patrick McHardy 已提交
92 93 94 95
	} else
		quantum = psched_mtu(qdisc_dev(sch));

	if (cl != NULL) {
96
		if (tca[TCA_RATE]) {
97 98
			err = gen_replace_estimator(&cl->bstats, NULL,
						    &cl->rate_est,
99 100
						    NULL,
						    qdisc_root_sleeping_running(sch),
101
						    tca[TCA_RATE]);
102 103
			if (err) {
				NL_SET_ERR_MSG(extack, "Failed to replace estimator");
104
				return err;
105
			}
106 107
		}

P
Patrick McHardy 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121
		sch_tree_lock(sch);
		if (tb[TCA_DRR_QUANTUM])
			cl->quantum = quantum;
		sch_tree_unlock(sch);

		return 0;
	}

	cl = kzalloc(sizeof(struct drr_class), GFP_KERNEL);
	if (cl == NULL)
		return -ENOBUFS;

	cl->common.classid = classid;
	cl->quantum	   = quantum;
122
	cl->qdisc	   = qdisc_create_dflt(sch->dev_queue,
123 124
					       &pfifo_qdisc_ops, classid,
					       NULL);
P
Patrick McHardy 已提交
125 126
	if (cl->qdisc == NULL)
		cl->qdisc = &noop_qdisc;
127 128
	else
		qdisc_hash_add(cl->qdisc, true);
P
Patrick McHardy 已提交
129

130
	if (tca[TCA_RATE]) {
131
		err = gen_replace_estimator(&cl->bstats, NULL, &cl->rate_est,
132 133
					    NULL,
					    qdisc_root_sleeping_running(sch),
134 135
					    tca[TCA_RATE]);
		if (err) {
136
			NL_SET_ERR_MSG(extack, "Failed to replace estimator");
137
			qdisc_put(cl->qdisc);
138 139 140 141
			kfree(cl);
			return err;
		}
	}
P
Patrick McHardy 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154

	sch_tree_lock(sch);
	qdisc_class_hash_insert(&q->clhash, &cl->common);
	sch_tree_unlock(sch);

	qdisc_class_hash_grow(sch, &q->clhash);

	*arg = (unsigned long)cl;
	return 0;
}

static void drr_destroy_class(struct Qdisc *sch, struct drr_class *cl)
{
155
	gen_kill_estimator(&cl->rate_est);
156
	qdisc_put(cl->qdisc);
P
Patrick McHardy 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	kfree(cl);
}

static int drr_delete_class(struct Qdisc *sch, unsigned long arg)
{
	struct drr_sched *q = qdisc_priv(sch);
	struct drr_class *cl = (struct drr_class *)arg;

	if (cl->filter_cnt > 0)
		return -EBUSY;

	sch_tree_lock(sch);

	drr_purge_queue(cl);
	qdisc_class_hash_remove(&q->clhash, &cl->common);

	sch_tree_unlock(sch);

175 176
	drr_destroy_class(sch, cl);
	return 0;
P
Patrick McHardy 已提交
177 178
}

179
static unsigned long drr_search_class(struct Qdisc *sch, u32 classid)
P
Patrick McHardy 已提交
180
{
181
	return (unsigned long)drr_find_class(sch, classid);
P
Patrick McHardy 已提交
182 183
}

184 185
static struct tcf_block *drr_tcf_block(struct Qdisc *sch, unsigned long cl,
				       struct netlink_ext_ack *extack)
P
Patrick McHardy 已提交
186 187 188
{
	struct drr_sched *q = qdisc_priv(sch);

189 190
	if (cl) {
		NL_SET_ERR_MSG(extack, "DRR classid must be zero");
P
Patrick McHardy 已提交
191
		return NULL;
192
	}
P
Patrick McHardy 已提交
193

194
	return q->block;
P
Patrick McHardy 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
}

static unsigned long drr_bind_tcf(struct Qdisc *sch, unsigned long parent,
				  u32 classid)
{
	struct drr_class *cl = drr_find_class(sch, classid);

	if (cl != NULL)
		cl->filter_cnt++;

	return (unsigned long)cl;
}

static void drr_unbind_tcf(struct Qdisc *sch, unsigned long arg)
{
	struct drr_class *cl = (struct drr_class *)arg;

	cl->filter_cnt--;
}

static int drr_graft_class(struct Qdisc *sch, unsigned long arg,
216 217
			   struct Qdisc *new, struct Qdisc **old,
			   struct netlink_ext_ack *extack)
P
Patrick McHardy 已提交
218 219 220 221
{
	struct drr_class *cl = (struct drr_class *)arg;

	if (new == NULL) {
222 223
		new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
					cl->common.classid, NULL);
P
Patrick McHardy 已提交
224 225 226 227
		if (new == NULL)
			new = &noop_qdisc;
	}

228
	*old = qdisc_replace(sch, new, &cl->qdisc);
P
Patrick McHardy 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242
	return 0;
}

static struct Qdisc *drr_class_leaf(struct Qdisc *sch, unsigned long arg)
{
	struct drr_class *cl = (struct drr_class *)arg;

	return cl->qdisc;
}

static void drr_qlen_notify(struct Qdisc *csh, unsigned long arg)
{
	struct drr_class *cl = (struct drr_class *)arg;

243
	list_del(&cl->alist);
P
Patrick McHardy 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
}

static int drr_dump_class(struct Qdisc *sch, unsigned long arg,
			  struct sk_buff *skb, struct tcmsg *tcm)
{
	struct drr_class *cl = (struct drr_class *)arg;
	struct nlattr *nest;

	tcm->tcm_parent	= TC_H_ROOT;
	tcm->tcm_handle	= cl->common.classid;
	tcm->tcm_info	= cl->qdisc->handle;

	nest = nla_nest_start(skb, TCA_OPTIONS);
	if (nest == NULL)
		goto nla_put_failure;
259 260
	if (nla_put_u32(skb, TCA_DRR_QUANTUM, cl->quantum))
		goto nla_put_failure;
P
Patrick McHardy 已提交
261 262 263 264 265 266 267 268 269 270 271
	return nla_nest_end(skb, nest);

nla_put_failure:
	nla_nest_cancel(skb, nest);
	return -EMSGSIZE;
}

static int drr_dump_class_stats(struct Qdisc *sch, unsigned long arg,
				struct gnet_dump *d)
{
	struct drr_class *cl = (struct drr_class *)arg;
272
	__u32 qlen = cl->qdisc->q.qlen;
P
Patrick McHardy 已提交
273 274 275
	struct tc_drr_stats xstats;

	memset(&xstats, 0, sizeof(xstats));
276
	if (qlen)
P
Patrick McHardy 已提交
277 278
		xstats.deficit = cl->deficit;

279 280
	if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch),
				  d, NULL, &cl->bstats) < 0 ||
281
	    gnet_stats_copy_rate_est(d, &cl->rate_est) < 0 ||
282
	    gnet_stats_copy_queue(d, NULL, &cl->qdisc->qstats, qlen) < 0)
P
Patrick McHardy 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
		return -1;

	return gnet_stats_copy_app(d, &xstats, sizeof(xstats));
}

static void drr_walk(struct Qdisc *sch, struct qdisc_walker *arg)
{
	struct drr_sched *q = qdisc_priv(sch);
	struct drr_class *cl;
	unsigned int i;

	if (arg->stop)
		return;

	for (i = 0; i < q->clhash.hashsize; i++) {
298
		hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode) {
P
Patrick McHardy 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
			if (arg->count < arg->skip) {
				arg->count++;
				continue;
			}
			if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
				arg->stop = 1;
				return;
			}
			arg->count++;
		}
	}
}

static struct drr_class *drr_classify(struct sk_buff *skb, struct Qdisc *sch,
				      int *qerr)
{
	struct drr_sched *q = qdisc_priv(sch);
	struct drr_class *cl;
	struct tcf_result res;
J
John Fastabend 已提交
318
	struct tcf_proto *fl;
P
Patrick McHardy 已提交
319 320 321 322 323 324 325 326 327
	int result;

	if (TC_H_MAJ(skb->priority ^ sch->handle) == 0) {
		cl = drr_find_class(sch, skb->priority);
		if (cl != NULL)
			return cl;
	}

	*qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
J
John Fastabend 已提交
328
	fl = rcu_dereference_bh(q->filter_list);
329
	result = tcf_classify(skb, fl, &res, false);
P
Patrick McHardy 已提交
330 331 332 333 334
	if (result >= 0) {
#ifdef CONFIG_NET_CLS_ACT
		switch (result) {
		case TC_ACT_QUEUED:
		case TC_ACT_STOLEN:
335
		case TC_ACT_TRAP:
P
Patrick McHardy 已提交
336
			*qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
337
			/* fall through */
P
Patrick McHardy 已提交
338 339 340 341 342 343 344 345 346 347 348 349
		case TC_ACT_SHOT:
			return NULL;
		}
#endif
		cl = (struct drr_class *)res.class;
		if (cl == NULL)
			cl = drr_find_class(sch, res.classid);
		return cl;
	}
	return NULL;
}

350 351
static int drr_enqueue(struct sk_buff *skb, struct Qdisc *sch,
		       struct sk_buff **to_free)
P
Patrick McHardy 已提交
352 353 354
{
	struct drr_sched *q = qdisc_priv(sch);
	struct drr_class *cl;
355
	int err = 0;
P
Patrick McHardy 已提交
356 357 358 359

	cl = drr_classify(skb, sch, &err);
	if (cl == NULL) {
		if (err & __NET_XMIT_BYPASS)
360
			qdisc_qstats_drop(sch);
361
		__qdisc_drop(skb, to_free);
P
Patrick McHardy 已提交
362 363 364
		return err;
	}

365
	err = qdisc_enqueue(skb, cl->qdisc, to_free);
P
Patrick McHardy 已提交
366 367 368
	if (unlikely(err != NET_XMIT_SUCCESS)) {
		if (net_xmit_drop_count(err)) {
			cl->qstats.drops++;
369
			qdisc_qstats_drop(sch);
P
Patrick McHardy 已提交
370 371 372 373 374 375 376 377 378
		}
		return err;
	}

	if (cl->qdisc->q.qlen == 1) {
		list_add_tail(&cl->alist, &q->active);
		cl->deficit = cl->quantum;
	}

W
WANG Cong 已提交
379
	qdisc_qstats_backlog_inc(sch, skb);
P
Patrick McHardy 已提交
380 381 382 383 384 385 386 387 388 389 390
	sch->q.qlen++;
	return err;
}

static struct sk_buff *drr_dequeue(struct Qdisc *sch)
{
	struct drr_sched *q = qdisc_priv(sch);
	struct drr_class *cl;
	struct sk_buff *skb;
	unsigned int len;

391 392 393
	if (list_empty(&q->active))
		goto out;
	while (1) {
P
Patrick McHardy 已提交
394 395
		cl = list_first_entry(&q->active, struct drr_class, alist);
		skb = cl->qdisc->ops->peek(cl->qdisc);
396 397
		if (skb == NULL) {
			qdisc_warn_nonwc(__func__, cl->qdisc);
398
			goto out;
399
		}
P
Patrick McHardy 已提交
400 401 402 403 404

		len = qdisc_pkt_len(skb);
		if (len <= cl->deficit) {
			cl->deficit -= len;
			skb = qdisc_dequeue_peeked(cl->qdisc);
405 406
			if (unlikely(skb == NULL))
				goto out;
P
Patrick McHardy 已提交
407 408
			if (cl->qdisc->q.qlen == 0)
				list_del(&cl->alist);
409 410

			bstats_update(&cl->bstats, skb);
411
			qdisc_bstats_update(sch, skb);
W
WANG Cong 已提交
412
			qdisc_qstats_backlog_dec(sch, skb);
P
Patrick McHardy 已提交
413 414 415 416 417 418 419
			sch->q.qlen--;
			return skb;
		}

		cl->deficit += cl->quantum;
		list_move_tail(&cl->alist, &q->active);
	}
420
out:
P
Patrick McHardy 已提交
421 422 423
	return NULL;
}

424 425
static int drr_init_qdisc(struct Qdisc *sch, struct nlattr *opt,
			  struct netlink_ext_ack *extack)
P
Patrick McHardy 已提交
426 427 428 429
{
	struct drr_sched *q = qdisc_priv(sch);
	int err;

430
	err = tcf_block_get(&q->block, &q->filter_list, sch, extack);
431 432
	if (err)
		return err;
P
Patrick McHardy 已提交
433 434 435 436 437 438 439 440 441 442 443 444 445 446
	err = qdisc_class_hash_init(&q->clhash);
	if (err < 0)
		return err;
	INIT_LIST_HEAD(&q->active);
	return 0;
}

static void drr_reset_qdisc(struct Qdisc *sch)
{
	struct drr_sched *q = qdisc_priv(sch);
	struct drr_class *cl;
	unsigned int i;

	for (i = 0; i < q->clhash.hashsize; i++) {
447
		hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode) {
P
Patrick McHardy 已提交
448 449 450 451 452
			if (cl->qdisc->q.qlen)
				list_del(&cl->alist);
			qdisc_reset(cl->qdisc);
		}
	}
W
WANG Cong 已提交
453
	sch->qstats.backlog = 0;
P
Patrick McHardy 已提交
454 455 456 457 458 459 460
	sch->q.qlen = 0;
}

static void drr_destroy_qdisc(struct Qdisc *sch)
{
	struct drr_sched *q = qdisc_priv(sch);
	struct drr_class *cl;
461
	struct hlist_node *next;
P
Patrick McHardy 已提交
462 463
	unsigned int i;

464
	tcf_block_put(q->block);
P
Patrick McHardy 已提交
465 466

	for (i = 0; i < q->clhash.hashsize; i++) {
467
		hlist_for_each_entry_safe(cl, next, &q->clhash.hash[i],
P
Patrick McHardy 已提交
468 469 470 471 472 473 474 475 476
					  common.hnode)
			drr_destroy_class(sch, cl);
	}
	qdisc_class_hash_destroy(&q->clhash);
}

static const struct Qdisc_class_ops drr_class_ops = {
	.change		= drr_change_class,
	.delete		= drr_delete_class,
477
	.find		= drr_search_class,
478
	.tcf_block	= drr_tcf_block,
P
Patrick McHardy 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
	.bind_tcf	= drr_bind_tcf,
	.unbind_tcf	= drr_unbind_tcf,
	.graft		= drr_graft_class,
	.leaf		= drr_class_leaf,
	.qlen_notify	= drr_qlen_notify,
	.dump		= drr_dump_class,
	.dump_stats	= drr_dump_class_stats,
	.walk		= drr_walk,
};

static struct Qdisc_ops drr_qdisc_ops __read_mostly = {
	.cl_ops		= &drr_class_ops,
	.id		= "drr",
	.priv_size	= sizeof(struct drr_sched),
	.enqueue	= drr_enqueue,
	.dequeue	= drr_dequeue,
	.peek		= qdisc_peek_dequeued,
	.init		= drr_init_qdisc,
	.reset		= drr_reset_qdisc,
	.destroy	= drr_destroy_qdisc,
	.owner		= THIS_MODULE,
};

static int __init drr_init(void)
{
	return register_qdisc(&drr_qdisc_ops);
}

static void __exit drr_exit(void)
{
	unregister_qdisc(&drr_qdisc_ops);
}

module_init(drr_init);
module_exit(drr_exit);
MODULE_LICENSE("GPL");