xref: /linux/net/sched/sch_api.c (revision 59e6295fac26b8e85c1ea859cdd89fa1e47519d7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * net/sched/sch_api.c	Packet scheduler API.
4  *
5  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6  *
7  * Fixes:
8  *
9  * Rani Assaf <rani@magic.metawire.com> :980802: JIFFIES and CPU clock sources are repaired.
10  * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
11  * Jamal Hadi Salim <hadi@nortelnetworks.com>: 990601: ingress support
12  */
13 
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/skbuff.h>
20 #include <linux/init.h>
21 #include <linux/proc_fs.h>
22 #include <linux/seq_file.h>
23 #include <linux/kmod.h>
24 #include <linux/list.h>
25 #include <linux/hrtimer.h>
26 #include <linux/slab.h>
27 #include <linux/hashtable.h>
28 #include <linux/bpf.h>
29 
30 #include <net/netdev_lock.h>
31 #include <net/net_namespace.h>
32 #include <net/sock.h>
33 #include <net/netlink.h>
34 #include <net/pkt_sched.h>
35 #include <net/pkt_cls.h>
36 #include <net/tc_wrapper.h>
37 
38 #include <trace/events/qdisc.h>
39 
40 /*
41 
42    Short review.
43    -------------
44 
45    This file consists of two interrelated parts:
46 
47    1. queueing disciplines manager frontend.
48    2. traffic classes manager frontend.
49 
50    Generally, queueing discipline ("qdisc") is a black box,
51    which is able to enqueue packets and to dequeue them (when
52    device is ready to send something) in order and at times
53    determined by algorithm hidden in it.
54 
55    qdisc's are divided to two categories:
56    - "queues", which have no internal structure visible from outside.
57    - "schedulers", which split all the packets to "traffic classes",
58      using "packet classifiers" (look at cls_api.c)
59 
60    In turn, classes may have child qdiscs (as rule, queues)
61    attached to them etc. etc. etc.
62 
63    The goal of the routines in this file is to translate
64    information supplied by user in the form of handles
65    to more intelligible for kernel form, to make some sanity
66    checks and part of work, which is common to all qdiscs
67    and to provide rtnetlink notifications.
68 
69    All real intelligent work is done inside qdisc modules.
70 
71 
72 
73    Every discipline has two major routines: enqueue and dequeue.
74 
75    ---dequeue
76 
77    dequeue usually returns a skb to send. It is allowed to return NULL,
78    but it does not mean that queue is empty, it just means that
79    discipline does not want to send anything this time.
80    Queue is really empty if q->q.qlen == 0.
81    For complicated disciplines with multiple queues q->q is not
82    real packet queue, but however q->q.qlen must be valid.
83 
84    ---enqueue
85 
86    enqueue returns 0, if packet was enqueued successfully.
87    If packet (this one or another one) was dropped, it returns
88    not zero error code.
89    NET_XMIT_DROP 	- this packet dropped
90      Expected action: do not backoff, but wait until queue will clear.
91    NET_XMIT_CN	 	- probably this packet enqueued, but another one dropped.
92      Expected action: backoff or ignore
93 
94    Auxiliary routines:
95 
96    ---peek
97 
98    like dequeue but without removing a packet from the queue
99 
100    ---reset
101 
102    returns qdisc to initial state: purge all buffers, clear all
103    timers, counters (except for statistics) etc.
104 
105    ---init
106 
107    initializes newly created qdisc.
108 
109    ---destroy
110 
111    destroys resources allocated by init and during lifetime of qdisc.
112 
113    ---change
114 
115    changes qdisc parameters.
116  */
117 
118 /* Protects list of registered TC modules. It is pure SMP lock. */
119 static DEFINE_RWLOCK(qdisc_mod_lock);
120 
121 
122 /************************************************
123  *	Queueing disciplines manipulation.	*
124  ************************************************/
125 
126 
127 /* The list of all installed queueing disciplines. */
128 
129 static struct Qdisc_ops *qdisc_base;
130 
131 /* Register/unregister queueing discipline */
132 
133 int register_qdisc(struct Qdisc_ops *qops)
134 {
135 	struct Qdisc_ops *q, **qp;
136 	int rc = -EEXIST;
137 
138 	write_lock(&qdisc_mod_lock);
139 	for (qp = &qdisc_base; (q = *qp) != NULL; qp = &q->next)
140 		if (!strcmp(qops->id, q->id))
141 			goto out;
142 
143 	if (qops->enqueue == NULL)
144 		qops->enqueue = noop_qdisc_ops.enqueue;
145 	if (qops->peek == NULL) {
146 		if (qops->dequeue == NULL)
147 			qops->peek = noop_qdisc_ops.peek;
148 		else
149 			goto out_einval;
150 	}
151 	if (qops->dequeue == NULL)
152 		qops->dequeue = noop_qdisc_ops.dequeue;
153 
154 	if (qops->cl_ops) {
155 		const struct Qdisc_class_ops *cops = qops->cl_ops;
156 
157 		if (!(cops->find && cops->walk && cops->leaf))
158 			goto out_einval;
159 
160 		if (cops->tcf_block && !(cops->bind_tcf && cops->unbind_tcf))
161 			goto out_einval;
162 	}
163 
164 	qops->next = NULL;
165 	*qp = qops;
166 	rc = 0;
167 out:
168 	write_unlock(&qdisc_mod_lock);
169 	return rc;
170 
171 out_einval:
172 	rc = -EINVAL;
173 	goto out;
174 }
175 EXPORT_SYMBOL(register_qdisc);
176 
177 void unregister_qdisc(struct Qdisc_ops *qops)
178 {
179 	struct Qdisc_ops *q, **qp;
180 	int err = -ENOENT;
181 
182 	write_lock(&qdisc_mod_lock);
183 	for (qp = &qdisc_base; (q = *qp) != NULL; qp = &q->next)
184 		if (q == qops)
185 			break;
186 	if (q) {
187 		*qp = q->next;
188 		q->next = NULL;
189 		err = 0;
190 	}
191 	write_unlock(&qdisc_mod_lock);
192 
193 	WARN(err, "unregister qdisc(%s) failed\n", qops->id);
194 }
195 EXPORT_SYMBOL(unregister_qdisc);
196 
197 /* Get default qdisc if not otherwise specified */
198 void qdisc_get_default(char *name, size_t len)
199 {
200 	read_lock(&qdisc_mod_lock);
201 	strscpy(name, default_qdisc_ops->id, len);
202 	read_unlock(&qdisc_mod_lock);
203 }
204 
205 static struct Qdisc_ops *qdisc_lookup_default(const char *name)
206 {
207 	struct Qdisc_ops *q = NULL;
208 
209 	for (q = qdisc_base; q; q = q->next) {
210 		if (!strcmp(name, q->id)) {
211 			if (!bpf_try_module_get(q, q->owner))
212 				q = NULL;
213 			break;
214 		}
215 	}
216 
217 	return q;
218 }
219 
220 /* Set new default qdisc to use */
221 int qdisc_set_default(const char *name)
222 {
223 	const struct Qdisc_ops *ops;
224 
225 	if (!capable(CAP_NET_ADMIN))
226 		return -EPERM;
227 
228 	write_lock(&qdisc_mod_lock);
229 	ops = qdisc_lookup_default(name);
230 	if (!ops) {
231 		/* Not found, drop lock and try to load module */
232 		write_unlock(&qdisc_mod_lock);
233 		request_module(NET_SCH_ALIAS_PREFIX "%s", name);
234 		write_lock(&qdisc_mod_lock);
235 
236 		ops = qdisc_lookup_default(name);
237 	}
238 
239 	if (ops) {
240 		/* Set new default */
241 		bpf_module_put(default_qdisc_ops, default_qdisc_ops->owner);
242 		default_qdisc_ops = ops;
243 	}
244 	write_unlock(&qdisc_mod_lock);
245 
246 	return ops ? 0 : -ENOENT;
247 }
248 
249 #ifdef CONFIG_NET_SCH_DEFAULT
250 /* Set default value from kernel config */
251 static int __init sch_default_qdisc(void)
252 {
253 	return qdisc_set_default(CONFIG_DEFAULT_NET_SCH);
254 }
255 late_initcall(sch_default_qdisc);
256 #endif
257 
258 /* We know handle. Find qdisc among all qdisc's attached to device
259  * (root qdisc, all its children, children of children etc.)
260  * Note: caller either uses rtnl or rcu_read_lock()
261  */
262 
263 static struct Qdisc *qdisc_match_from_root(struct Qdisc *root, u32 handle)
264 {
265 	struct Qdisc *q;
266 
267 	if (!qdisc_dev(root))
268 		return (root->handle == handle ? root : NULL);
269 
270 	if (!(root->flags & TCQ_F_BUILTIN) &&
271 	    root->handle == handle)
272 		return root;
273 
274 	hash_for_each_possible_rcu(qdisc_dev(root)->qdisc_hash, q, hash, handle,
275 				   lockdep_rtnl_is_held()) {
276 		if (q->handle == handle)
277 			return q;
278 	}
279 	return NULL;
280 }
281 
282 void qdisc_hash_add(struct Qdisc *q, bool invisible)
283 {
284 	if ((q->parent != TC_H_ROOT) && !(q->flags & TCQ_F_INGRESS)) {
285 		ASSERT_RTNL();
286 		hash_add_rcu(qdisc_dev(q)->qdisc_hash, &q->hash, q->handle);
287 		if (invisible)
288 			q->flags |= TCQ_F_INVISIBLE;
289 	}
290 }
291 EXPORT_SYMBOL(qdisc_hash_add);
292 
293 void qdisc_hash_del(struct Qdisc *q)
294 {
295 	if ((q->parent != TC_H_ROOT) && !(q->flags & TCQ_F_INGRESS)) {
296 		ASSERT_RTNL();
297 		hash_del_rcu(&q->hash);
298 	}
299 }
300 EXPORT_SYMBOL(qdisc_hash_del);
301 
302 struct Qdisc *qdisc_lookup(struct net_device *dev, u32 handle)
303 {
304 	struct Qdisc *q;
305 
306 	if (!handle)
307 		return NULL;
308 	q = qdisc_match_from_root(rtnl_dereference(dev->qdisc), handle);
309 	if (q)
310 		goto out;
311 
312 	if (dev_ingress_queue(dev))
313 		q = qdisc_match_from_root(
314 			rtnl_dereference(dev_ingress_queue(dev)->qdisc_sleeping),
315 			handle);
316 out:
317 	return q;
318 }
319 
320 struct Qdisc *qdisc_lookup_rcu(struct net_device *dev, u32 handle)
321 {
322 	struct netdev_queue *nq;
323 	struct Qdisc *q;
324 
325 	if (!handle)
326 		return NULL;
327 	q = qdisc_match_from_root(rcu_dereference(dev->qdisc), handle);
328 	if (q)
329 		goto out;
330 
331 	nq = dev_ingress_queue_rcu(dev);
332 	if (nq)
333 		q = qdisc_match_from_root(rcu_dereference(nq->qdisc_sleeping),
334 					  handle);
335 out:
336 	return q;
337 }
338 
339 static struct Qdisc *qdisc_leaf(struct Qdisc *p, u32 classid,
340 				struct netlink_ext_ack *extack)
341 {
342 	unsigned long cl;
343 	const struct Qdisc_class_ops *cops = p->ops->cl_ops;
344 
345 	if (cops == NULL) {
346 		NL_SET_ERR_MSG(extack, "Parent qdisc is not classful");
347 		return ERR_PTR(-EOPNOTSUPP);
348 	}
349 	cl = cops->find(p, classid);
350 
351 	if (cl == 0) {
352 		NL_SET_ERR_MSG(extack, "Specified class not found");
353 		return ERR_PTR(-ENOENT);
354 	}
355 	return cops->leaf(p, cl);
356 }
357 
358 /* Find queueing discipline by name */
359 
360 static struct Qdisc_ops *qdisc_lookup_ops(struct nlattr *kind)
361 {
362 	struct Qdisc_ops *q = NULL;
363 
364 	if (kind) {
365 		read_lock(&qdisc_mod_lock);
366 		for (q = qdisc_base; q; q = q->next) {
367 			if (nla_strcmp(kind, q->id) == 0) {
368 				if (!bpf_try_module_get(q, q->owner))
369 					q = NULL;
370 				break;
371 			}
372 		}
373 		read_unlock(&qdisc_mod_lock);
374 	}
375 	return q;
376 }
377 
378 /* The linklayer setting were not transferred from iproute2, in older
379  * versions, and the rate tables lookup systems have been dropped in
380  * the kernel. To keep backward compatible with older iproute2 tc
381  * utils, we detect the linklayer setting by detecting if the rate
382  * table were modified.
383  *
384  * For linklayer ATM table entries, the rate table will be aligned to
385  * 48 bytes, thus some table entries will contain the same value.  The
386  * mpu (min packet unit) is also encoded into the old rate table, thus
387  * starting from the mpu, we find low and high table entries for
388  * mapping this cell.  If these entries contain the same value, when
389  * the rate tables have been modified for linklayer ATM.
390  *
391  * This is done by rounding mpu to the nearest 48 bytes cell/entry,
392  * and then roundup to the next cell, calc the table entry one below,
393  * and compare.
394  */
395 static __u8 __detect_linklayer(struct tc_ratespec *r, __u32 *rtab)
396 {
397 	int low       = roundup(r->mpu, 48);
398 	int high      = roundup(low+1, 48);
399 	int cell_low  = low >> r->cell_log;
400 	int cell_high = (high >> r->cell_log) - 1;
401 
402 	/* rtab is too inaccurate at rates > 100Mbit/s */
403 	if ((r->rate > (100000000/8)) || (rtab[0] == 0)) {
404 		pr_debug("TC linklayer: Giving up ATM detection\n");
405 		return TC_LINKLAYER_ETHERNET;
406 	}
407 
408 	if ((cell_high > cell_low) && (cell_high < 256)
409 	    && (rtab[cell_low] == rtab[cell_high])) {
410 		pr_debug("TC linklayer: Detected ATM, low(%d)=high(%d)=%u\n",
411 			 cell_low, cell_high, rtab[cell_high]);
412 		return TC_LINKLAYER_ATM;
413 	}
414 	return TC_LINKLAYER_ETHERNET;
415 }
416 
417 static struct qdisc_rate_table *qdisc_rtab_list;
418 static DEFINE_SPINLOCK(qdisc_rtab_lock);
419 
420 struct qdisc_rate_table *qdisc_get_rtab(struct tc_ratespec *r,
421 					struct nlattr *tab,
422 					struct netlink_ext_ack *extack)
423 {
424 	struct qdisc_rate_table *rtab, *new_rtab;
425 
426 	if (tab == NULL || r->rate == 0 ||
427 	    r->cell_log == 0 || r->cell_log >= 32 ||
428 	    nla_len(tab) != TC_RTAB_SIZE) {
429 		NL_SET_ERR_MSG(extack, "Invalid rate table parameters for searching");
430 		return NULL;
431 	}
432 
433 	new_rtab = kmalloc_obj(*new_rtab);
434 
435 	spin_lock(&qdisc_rtab_lock);
436 	for (rtab = qdisc_rtab_list; rtab; rtab = rtab->next) {
437 		if (!memcmp(&rtab->rate, r, sizeof(struct tc_ratespec)) &&
438 		    !memcmp(&rtab->data, nla_data(tab), TC_RTAB_SIZE)) {
439 			rtab->refcnt++;
440 			spin_unlock(&qdisc_rtab_lock);
441 			kfree(new_rtab);
442 			return rtab;
443 		}
444 	}
445 
446 	rtab = new_rtab;
447 	if (rtab) {
448 		rtab->rate = *r;
449 		rtab->refcnt = 1;
450 		memcpy(rtab->data, nla_data(tab), TC_RTAB_SIZE);
451 		if (r->linklayer == TC_LINKLAYER_UNAWARE)
452 			r->linklayer = __detect_linklayer(r, rtab->data);
453 		rtab->next = qdisc_rtab_list;
454 		qdisc_rtab_list = rtab;
455 	} else {
456 		NL_SET_ERR_MSG(extack, "Failed to allocate new qdisc rate table");
457 	}
458 	spin_unlock(&qdisc_rtab_lock);
459 	return rtab;
460 }
461 EXPORT_SYMBOL(qdisc_get_rtab);
462 
463 void qdisc_put_rtab(struct qdisc_rate_table *tab)
464 {
465 	struct qdisc_rate_table *rtab, **rtabp;
466 
467 	if (!tab)
468 		return;
469 
470 	spin_lock(&qdisc_rtab_lock);
471 	if (--tab->refcnt) {
472 		spin_unlock(&qdisc_rtab_lock);
473 		return;
474 	}
475 
476 	for (rtabp = &qdisc_rtab_list;
477 	     (rtab = *rtabp) != NULL;
478 	     rtabp = &rtab->next) {
479 		if (rtab == tab) {
480 			*rtabp = rtab->next;
481 			break;
482 		}
483 	}
484 	spin_unlock(&qdisc_rtab_lock);
485 	kfree(tab);
486 }
487 EXPORT_SYMBOL(qdisc_put_rtab);
488 
489 static LIST_HEAD(qdisc_stab_list);
490 
491 static const struct nla_policy stab_policy[TCA_STAB_MAX + 1] = {
492 	[TCA_STAB_BASE]	= { .len = sizeof(struct tc_sizespec) },
493 	[TCA_STAB_DATA] = { .type = NLA_BINARY },
494 };
495 
496 static struct qdisc_size_table *qdisc_get_stab(struct nlattr *opt,
497 					       struct netlink_ext_ack *extack)
498 {
499 	struct nlattr *tb[TCA_STAB_MAX + 1];
500 	struct qdisc_size_table *stab;
501 	struct tc_sizespec *s;
502 	unsigned int tsize = 0;
503 	u16 *tab = NULL;
504 	int err;
505 
506 	err = nla_parse_nested_deprecated(tb, TCA_STAB_MAX, opt, stab_policy,
507 					  extack);
508 	if (err < 0)
509 		return ERR_PTR(err);
510 	if (!tb[TCA_STAB_BASE]) {
511 		NL_SET_ERR_MSG(extack, "Size table base attribute is missing");
512 		return ERR_PTR(-EINVAL);
513 	}
514 
515 	s = nla_data(tb[TCA_STAB_BASE]);
516 
517 	if (s->tsize > 0) {
518 		if (!tb[TCA_STAB_DATA]) {
519 			NL_SET_ERR_MSG(extack, "Size table data attribute is missing");
520 			return ERR_PTR(-EINVAL);
521 		}
522 		tab = nla_data(tb[TCA_STAB_DATA]);
523 		tsize = nla_len(tb[TCA_STAB_DATA]) / sizeof(u16);
524 	}
525 
526 	if (tsize != s->tsize || (!tab && tsize > 0)) {
527 		NL_SET_ERR_MSG(extack, "Invalid size of size table");
528 		return ERR_PTR(-EINVAL);
529 	}
530 
531 	list_for_each_entry(stab, &qdisc_stab_list, list) {
532 		if (memcmp(&stab->szopts, s, sizeof(*s)))
533 			continue;
534 		if (tsize > 0 &&
535 		    memcmp(stab->data, tab, flex_array_size(stab, data, tsize)))
536 			continue;
537 		stab->refcnt++;
538 		return stab;
539 	}
540 
541 	if (s->size_log > STAB_SIZE_LOG_MAX ||
542 	    s->cell_log > STAB_SIZE_LOG_MAX) {
543 		NL_SET_ERR_MSG(extack, "Invalid logarithmic size of size table");
544 		return ERR_PTR(-EINVAL);
545 	}
546 
547 	stab = kmalloc_flex(*stab, data, tsize);
548 	if (!stab)
549 		return ERR_PTR(-ENOMEM);
550 
551 	stab->refcnt = 1;
552 	stab->szopts = *s;
553 	if (tsize > 0)
554 		memcpy(stab->data, tab, flex_array_size(stab, data, tsize));
555 
556 	list_add_tail(&stab->list, &qdisc_stab_list);
557 
558 	return stab;
559 }
560 
561 void qdisc_put_stab(struct qdisc_size_table *tab)
562 {
563 	if (!tab)
564 		return;
565 
566 	if (--tab->refcnt == 0) {
567 		list_del(&tab->list);
568 		kfree_rcu(tab, rcu);
569 	}
570 }
571 EXPORT_SYMBOL(qdisc_put_stab);
572 
573 static int qdisc_dump_stab(struct sk_buff *skb, struct qdisc_size_table *stab)
574 {
575 	struct nlattr *nest;
576 
577 	nest = nla_nest_start_noflag(skb, TCA_STAB);
578 	if (nest == NULL)
579 		goto nla_put_failure;
580 	if (nla_put(skb, TCA_STAB_BASE, sizeof(stab->szopts), &stab->szopts))
581 		goto nla_put_failure;
582 	nla_nest_end(skb, nest);
583 
584 	return skb->len;
585 
586 nla_put_failure:
587 	return -1;
588 }
589 
590 void __qdisc_calculate_pkt_len(struct sk_buff *skb,
591 			       const struct qdisc_size_table *stab)
592 {
593 	int pkt_len, slot;
594 
595 	pkt_len = skb->len + stab->szopts.overhead;
596 	if (unlikely(!stab->szopts.tsize))
597 		goto out;
598 
599 	slot = pkt_len + stab->szopts.cell_align;
600 	if (unlikely(slot < 0))
601 		slot = 0;
602 
603 	slot >>= stab->szopts.cell_log;
604 	if (likely(slot < stab->szopts.tsize))
605 		pkt_len = stab->data[slot];
606 	else
607 		pkt_len = stab->data[stab->szopts.tsize - 1] *
608 				(slot / stab->szopts.tsize) +
609 				stab->data[slot % stab->szopts.tsize];
610 
611 	pkt_len <<= stab->szopts.size_log;
612 out:
613 	if (unlikely(pkt_len < 1))
614 		pkt_len = 1;
615 	qdisc_skb_cb(skb)->pkt_len = pkt_len;
616 }
617 
618 static enum hrtimer_restart qdisc_watchdog(struct hrtimer *timer)
619 {
620 	struct qdisc_watchdog *wd = container_of(timer, struct qdisc_watchdog,
621 						 timer);
622 
623 	rcu_read_lock();
624 	__netif_schedule(qdisc_root(wd->qdisc));
625 	rcu_read_unlock();
626 
627 	return HRTIMER_NORESTART;
628 }
629 
630 void qdisc_watchdog_init_clockid(struct qdisc_watchdog *wd, struct Qdisc *qdisc,
631 				 clockid_t clockid)
632 {
633 	hrtimer_setup(&wd->timer, qdisc_watchdog, clockid, HRTIMER_MODE_ABS_PINNED);
634 	wd->qdisc = qdisc;
635 }
636 EXPORT_SYMBOL(qdisc_watchdog_init_clockid);
637 
638 void qdisc_watchdog_init(struct qdisc_watchdog *wd, struct Qdisc *qdisc)
639 {
640 	qdisc_watchdog_init_clockid(wd, qdisc, CLOCK_MONOTONIC);
641 }
642 EXPORT_SYMBOL(qdisc_watchdog_init);
643 
644 void qdisc_watchdog_schedule_range_ns(struct qdisc_watchdog *wd, u64 expires,
645 				      u64 delta_ns)
646 {
647 	bool deactivated;
648 
649 	rcu_read_lock();
650 	deactivated = test_bit(__QDISC_STATE_DEACTIVATED,
651 			       &qdisc_root_sleeping(wd->qdisc)->state);
652 	rcu_read_unlock();
653 	if (deactivated)
654 		return;
655 
656 	if (hrtimer_is_queued(&wd->timer)) {
657 		u64 softexpires;
658 
659 		softexpires = ktime_to_ns(hrtimer_get_softexpires(&wd->timer));
660 		/* If timer is already set in [expires, expires + delta_ns],
661 		 * do not reprogram it.
662 		 */
663 		if (softexpires - expires <= delta_ns)
664 			return;
665 	}
666 
667 	hrtimer_start_range_ns(&wd->timer,
668 			       ns_to_ktime(expires),
669 			       delta_ns,
670 			       HRTIMER_MODE_ABS_PINNED);
671 }
672 EXPORT_SYMBOL(qdisc_watchdog_schedule_range_ns);
673 
674 void qdisc_watchdog_cancel(struct qdisc_watchdog *wd)
675 {
676 	hrtimer_cancel(&wd->timer);
677 }
678 EXPORT_SYMBOL(qdisc_watchdog_cancel);
679 
680 static struct hlist_head *qdisc_class_hash_alloc(unsigned int n)
681 {
682 	struct hlist_head *h;
683 	unsigned int i;
684 
685 	h = kvmalloc_objs(struct hlist_head, n);
686 
687 	if (h != NULL) {
688 		for (i = 0; i < n; i++)
689 			INIT_HLIST_HEAD(&h[i]);
690 	}
691 	return h;
692 }
693 
694 void qdisc_class_hash_grow(struct Qdisc *sch, struct Qdisc_class_hash *clhash)
695 {
696 	struct Qdisc_class_common *cl;
697 	struct hlist_node *next;
698 	struct hlist_head *nhash, *ohash;
699 	unsigned int nsize, nmask, osize;
700 	unsigned int i, h;
701 
702 	/* Rehash when load factor exceeds 0.75 */
703 	if (clhash->hashelems * 4 <= clhash->hashsize * 3)
704 		return;
705 	nsize = clhash->hashsize * 2;
706 	nmask = nsize - 1;
707 	nhash = qdisc_class_hash_alloc(nsize);
708 	if (nhash == NULL)
709 		return;
710 
711 	ohash = clhash->hash;
712 	osize = clhash->hashsize;
713 
714 	sch_tree_lock(sch);
715 	for (i = 0; i < osize; i++) {
716 		hlist_for_each_entry_safe(cl, next, &ohash[i], hnode) {
717 			h = qdisc_class_hash(cl->classid, nmask);
718 			hlist_add_head(&cl->hnode, &nhash[h]);
719 		}
720 	}
721 	clhash->hash     = nhash;
722 	clhash->hashsize = nsize;
723 	clhash->hashmask = nmask;
724 	sch_tree_unlock(sch);
725 
726 	kvfree(ohash);
727 }
728 EXPORT_SYMBOL(qdisc_class_hash_grow);
729 
730 int qdisc_class_hash_init(struct Qdisc_class_hash *clhash)
731 {
732 	unsigned int size = 4;
733 
734 	clhash->hash = qdisc_class_hash_alloc(size);
735 	if (!clhash->hash)
736 		return -ENOMEM;
737 	clhash->hashsize  = size;
738 	clhash->hashmask  = size - 1;
739 	clhash->hashelems = 0;
740 	return 0;
741 }
742 EXPORT_SYMBOL(qdisc_class_hash_init);
743 
744 void qdisc_class_hash_destroy(struct Qdisc_class_hash *clhash)
745 {
746 	kvfree(clhash->hash);
747 }
748 EXPORT_SYMBOL(qdisc_class_hash_destroy);
749 
750 void qdisc_class_hash_insert(struct Qdisc_class_hash *clhash,
751 			     struct Qdisc_class_common *cl)
752 {
753 	unsigned int h;
754 
755 	INIT_HLIST_NODE(&cl->hnode);
756 	h = qdisc_class_hash(cl->classid, clhash->hashmask);
757 	hlist_add_head(&cl->hnode, &clhash->hash[h]);
758 	clhash->hashelems++;
759 }
760 EXPORT_SYMBOL(qdisc_class_hash_insert);
761 
762 void qdisc_class_hash_remove(struct Qdisc_class_hash *clhash,
763 			     struct Qdisc_class_common *cl)
764 {
765 	hlist_del(&cl->hnode);
766 	clhash->hashelems--;
767 }
768 EXPORT_SYMBOL(qdisc_class_hash_remove);
769 
770 /* Allocate an unique handle from space managed by kernel
771  * Possible range is [8000-FFFF]:0000 (0x8000 values)
772  */
773 static u32 qdisc_alloc_handle(struct net_device *dev)
774 {
775 	int i = 0x8000;
776 	static u32 autohandle = TC_H_MAKE(0x80000000U, 0);
777 
778 	do {
779 		autohandle += TC_H_MAKE(0x10000U, 0);
780 		if (autohandle == TC_H_MAKE(TC_H_ROOT, 0))
781 			autohandle = TC_H_MAKE(0x80000000U, 0);
782 		if (!qdisc_lookup(dev, autohandle))
783 			return autohandle;
784 		cond_resched();
785 	} while	(--i > 0);
786 
787 	return 0;
788 }
789 
790 void qdisc_tree_reduce_backlog(struct Qdisc *sch, int n, int len)
791 {
792 	const struct Qdisc_class_ops *cops;
793 	unsigned long cl;
794 	u32 parentid;
795 	bool notify;
796 	int drops;
797 
798 	drops = max_t(int, n, 0);
799 	rcu_read_lock();
800 	while ((parentid = sch->parent)) {
801 		if (parentid == TC_H_ROOT)
802 			break;
803 
804 		if (sch->flags & TCQ_F_NOPARENT)
805 			break;
806 		/* Notify parent qdisc only if child qdisc becomes empty. */
807 		notify = !sch->q.qlen;
808 		/* TODO: perform the search on a per txq basis */
809 		sch = qdisc_lookup_rcu(qdisc_dev(sch), TC_H_MAJ(parentid));
810 		if (sch == NULL) {
811 			WARN_ON_ONCE(parentid != TC_H_ROOT);
812 			break;
813 		}
814 		cops = sch->ops->cl_ops;
815 		if (notify && cops->qlen_notify) {
816 			/* Note that qlen_notify must be idempotent as it may get called
817 			 * multiple times.
818 			 */
819 			cl = cops->find(sch, parentid);
820 			cops->qlen_notify(sch, cl);
821 		}
822 		WRITE_ONCE(sch->q.qlen, sch->q.qlen - n);
823 		qstats_backlog_sub(sch, len);
824 		__qdisc_qstats_drop(sch, drops);
825 	}
826 	rcu_read_unlock();
827 }
828 EXPORT_SYMBOL(qdisc_tree_reduce_backlog);
829 
830 int qdisc_offload_dump_helper(struct Qdisc *sch, enum tc_setup_type type,
831 			      void *type_data)
832 {
833 	struct net_device *dev = qdisc_dev(sch);
834 	int err;
835 
836 	sch->flags &= ~TCQ_F_OFFLOADED;
837 	if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
838 		return 0;
839 
840 	err = dev->netdev_ops->ndo_setup_tc(dev, type, type_data);
841 	if (err == -EOPNOTSUPP)
842 		return 0;
843 
844 	if (!err)
845 		sch->flags |= TCQ_F_OFFLOADED;
846 
847 	return err;
848 }
849 EXPORT_SYMBOL(qdisc_offload_dump_helper);
850 
851 void qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch,
852 				struct Qdisc *new, struct Qdisc *old,
853 				enum tc_setup_type type, void *type_data,
854 				struct netlink_ext_ack *extack)
855 {
856 	bool any_qdisc_is_offloaded;
857 	int err;
858 
859 	if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
860 		return;
861 
862 	err = dev->netdev_ops->ndo_setup_tc(dev, type, type_data);
863 
864 	/* Don't report error if the graft is part of destroy operation. */
865 	if (!err || !new || new == &noop_qdisc)
866 		return;
867 
868 	/* Don't report error if the parent, the old child and the new
869 	 * one are not offloaded.
870 	 */
871 	any_qdisc_is_offloaded = new->flags & TCQ_F_OFFLOADED;
872 	any_qdisc_is_offloaded |= sch && sch->flags & TCQ_F_OFFLOADED;
873 	any_qdisc_is_offloaded |= old && old->flags & TCQ_F_OFFLOADED;
874 
875 	if (any_qdisc_is_offloaded)
876 		NL_SET_ERR_MSG_WEAK(extack, "Offloading graft operation failed.");
877 }
878 EXPORT_SYMBOL(qdisc_offload_graft_helper);
879 
880 void qdisc_offload_query_caps(struct net_device *dev,
881 			      enum tc_setup_type type,
882 			      void *caps, size_t caps_len)
883 {
884 	const struct net_device_ops *ops = dev->netdev_ops;
885 	struct tc_query_caps_base base = {
886 		.type = type,
887 		.caps = caps,
888 	};
889 
890 	memset(caps, 0, caps_len);
891 
892 	if (ops->ndo_setup_tc)
893 		ops->ndo_setup_tc(dev, TC_QUERY_CAPS, &base);
894 }
895 EXPORT_SYMBOL(qdisc_offload_query_caps);
896 
897 static void qdisc_offload_graft_root(struct net_device *dev,
898 				     struct Qdisc *new, struct Qdisc *old,
899 				     struct netlink_ext_ack *extack)
900 {
901 	struct tc_root_qopt_offload graft_offload = {
902 		.command	= TC_ROOT_GRAFT,
903 		.handle		= new ? new->handle : 0,
904 		.ingress	= (new && new->flags & TCQ_F_INGRESS) ||
905 				  (old && old->flags & TCQ_F_INGRESS),
906 	};
907 
908 	qdisc_offload_graft_helper(dev, NULL, new, old,
909 				   TC_SETUP_ROOT_QDISC, &graft_offload, extack);
910 }
911 
912 static int tc_fill_qdisc(struct sk_buff *skb, struct Qdisc *q, u32 clid,
913 			 u32 portid, u32 seq, u16 flags, int event,
914 			 struct netlink_ext_ack *extack)
915 {
916 	struct gnet_stats_basic_sync __percpu *cpu_bstats = NULL;
917 	struct gnet_stats_queue __percpu *cpu_qstats = NULL;
918 	struct tcmsg *tcm;
919 	struct nlmsghdr  *nlh;
920 	unsigned char *b = skb_tail_pointer(skb);
921 	struct gnet_dump d;
922 	struct qdisc_size_table *stab;
923 	u32 block_index;
924 	__u32 qlen;
925 
926 	cond_resched();
927 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
928 	if (!nlh)
929 		goto out_nlmsg_trim;
930 	tcm = nlmsg_data(nlh);
931 	tcm->tcm_family = AF_UNSPEC;
932 	tcm->tcm__pad1 = 0;
933 	tcm->tcm__pad2 = 0;
934 	tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
935 	tcm->tcm_parent = clid;
936 	tcm->tcm_handle = q->handle;
937 	tcm->tcm_info = refcount_read(&q->refcnt);
938 	if (nla_put_string(skb, TCA_KIND, q->ops->id))
939 		goto nla_put_failure;
940 	if (q->ops->ingress_block_get) {
941 		block_index = q->ops->ingress_block_get(q);
942 		if (block_index &&
943 		    nla_put_u32(skb, TCA_INGRESS_BLOCK, block_index))
944 			goto nla_put_failure;
945 	}
946 	if (q->ops->egress_block_get) {
947 		block_index = q->ops->egress_block_get(q);
948 		if (block_index &&
949 		    nla_put_u32(skb, TCA_EGRESS_BLOCK, block_index))
950 			goto nla_put_failure;
951 	}
952 	if (q->ops->dump && q->ops->dump(q, skb) < 0)
953 		goto nla_put_failure;
954 	if (nla_put_u8(skb, TCA_HW_OFFLOAD, !!(q->flags & TCQ_F_OFFLOADED)))
955 		goto nla_put_failure;
956 	qlen = qdisc_qlen_sum(q);
957 
958 	stab = rtnl_dereference(q->stab);
959 	if (stab && qdisc_dump_stab(skb, stab) < 0)
960 		goto nla_put_failure;
961 
962 	if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS, TCA_XSTATS,
963 					 NULL, &d, TCA_PAD) < 0)
964 		goto nla_put_failure;
965 
966 	if (q->ops->dump_stats && q->ops->dump_stats(q, &d) < 0)
967 		goto nla_put_failure;
968 
969 	if (qdisc_is_percpu_stats(q)) {
970 		cpu_bstats = q->cpu_bstats;
971 		cpu_qstats = q->cpu_qstats;
972 	}
973 
974 	if (gnet_stats_copy_basic(&d, cpu_bstats, &q->bstats, true) < 0 ||
975 	    gnet_stats_copy_rate_est(&d, &q->rate_est) < 0 ||
976 	    gnet_stats_copy_queue(&d, cpu_qstats, &q->qstats, qlen) < 0)
977 		goto nla_put_failure;
978 
979 	if (gnet_stats_finish_copy(&d) < 0)
980 		goto nla_put_failure;
981 
982 	if (extack && extack->_msg &&
983 	    nla_put_string(skb, TCA_EXT_WARN_MSG, extack->_msg))
984 		goto out_nlmsg_trim;
985 
986 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
987 
988 	return skb->len;
989 
990 out_nlmsg_trim:
991 nla_put_failure:
992 	nlmsg_trim(skb, b);
993 	return -EMSGSIZE;
994 }
995 
996 static bool tc_qdisc_dump_ignore(struct Qdisc *q, bool dump_invisible,
997 				 const struct tcmsg *tcm)
998 {
999 	if (q->flags & TCQ_F_BUILTIN)
1000 		return true;
1001 	if ((q->flags & TCQ_F_INVISIBLE) && !dump_invisible)
1002 		return true;
1003 	if (tcm) {
1004 		if (tcm->tcm_handle && tcm->tcm_handle != q->handle)
1005 			return true;
1006 	}
1007 	return false;
1008 }
1009 
1010 static int qdisc_get_notify(struct net *net, struct sk_buff *oskb,
1011 			    struct nlmsghdr *n, u32 clid, struct Qdisc *q,
1012 			    struct netlink_ext_ack *extack)
1013 {
1014 	struct sk_buff *skb;
1015 	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1016 
1017 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1018 	if (!skb)
1019 		return -ENOBUFS;
1020 
1021 	if (!tc_qdisc_dump_ignore(q, false, NULL)) {
1022 		if (tc_fill_qdisc(skb, q, clid, portid, n->nlmsg_seq, 0,
1023 				  RTM_NEWQDISC, extack) < 0)
1024 			goto err_out;
1025 	}
1026 
1027 	if (skb->len)
1028 		return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1029 				      n->nlmsg_flags & NLM_F_ECHO);
1030 
1031 err_out:
1032 	kfree_skb(skb);
1033 	return -EINVAL;
1034 }
1035 
1036 static int qdisc_notify(struct net *net, struct sk_buff *oskb,
1037 			struct nlmsghdr *n, u32 clid,
1038 			struct Qdisc *old, struct Qdisc *new,
1039 			struct netlink_ext_ack *extack)
1040 {
1041 	struct sk_buff *skb;
1042 	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1043 
1044 	if (!rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC))
1045 		return 0;
1046 
1047 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1048 	if (!skb)
1049 		return -ENOBUFS;
1050 
1051 	if (old && !tc_qdisc_dump_ignore(old, false, NULL)) {
1052 		if (tc_fill_qdisc(skb, old, clid, portid, n->nlmsg_seq,
1053 				  0, RTM_DELQDISC, extack) < 0)
1054 			goto err_out;
1055 	}
1056 	if (new && !tc_qdisc_dump_ignore(new, false, NULL)) {
1057 		if (tc_fill_qdisc(skb, new, clid, portid, n->nlmsg_seq,
1058 				  old ? NLM_F_REPLACE : 0, RTM_NEWQDISC, extack) < 0)
1059 			goto err_out;
1060 	}
1061 
1062 	if (skb->len)
1063 		return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1064 				      n->nlmsg_flags & NLM_F_ECHO);
1065 
1066 err_out:
1067 	kfree_skb(skb);
1068 	return -EINVAL;
1069 }
1070 
1071 static void notify_and_destroy(struct net *net, struct sk_buff *skb,
1072 			       struct nlmsghdr *n, u32 clid,
1073 			       struct Qdisc *old, struct Qdisc *new,
1074 			       struct netlink_ext_ack *extack)
1075 {
1076 	if (new || old)
1077 		qdisc_notify(net, skb, n, clid, old, new, extack);
1078 
1079 	if (old)
1080 		qdisc_put(old);
1081 }
1082 
1083 static void qdisc_clear_nolock(struct Qdisc *sch)
1084 {
1085 	sch->flags &= ~TCQ_F_NOLOCK;
1086 	if (!(sch->flags & TCQ_F_CPUSTATS))
1087 		return;
1088 
1089 	free_percpu(sch->cpu_bstats);
1090 	free_percpu(sch->cpu_qstats);
1091 	sch->cpu_bstats = NULL;
1092 	sch->cpu_qstats = NULL;
1093 	sch->flags &= ~TCQ_F_CPUSTATS;
1094 }
1095 
1096 /* Graft qdisc "new" to class "classid" of qdisc "parent" or
1097  * to device "dev".
1098  *
1099  * When appropriate send a netlink notification using 'skb'
1100  * and "n".
1101  *
1102  * On success, destroy old qdisc.
1103  */
1104 
1105 static int qdisc_graft(struct net_device *dev, struct Qdisc *parent,
1106 		       struct sk_buff *skb, struct nlmsghdr *n, u32 classid,
1107 		       struct Qdisc *new, struct Qdisc *old,
1108 		       struct netlink_ext_ack *extack)
1109 {
1110 	struct Qdisc *q = old;
1111 	struct net *net = dev_net(dev);
1112 
1113 	if (parent == NULL) {
1114 		unsigned int i, num_q, ingress;
1115 		struct netdev_queue *dev_queue;
1116 
1117 		if (new)
1118 			new->depth = 0;
1119 
1120 		ingress = 0;
1121 		num_q = dev->num_tx_queues;
1122 		if ((q && q->flags & TCQ_F_INGRESS) ||
1123 		    (new && new->flags & TCQ_F_INGRESS)) {
1124 			ingress = 1;
1125 			dev_queue = dev_ingress_queue(dev);
1126 			if (!dev_queue) {
1127 				NL_SET_ERR_MSG(extack, "Device does not have an ingress queue");
1128 				return -ENOENT;
1129 			}
1130 
1131 			q = rtnl_dereference(dev_queue->qdisc_sleeping);
1132 
1133 			/* This is the counterpart of that qdisc_refcount_inc_nz() call in
1134 			 * __tcf_qdisc_find() for filter requests.
1135 			 */
1136 			if (!qdisc_refcount_dec_if_one(q)) {
1137 				NL_SET_ERR_MSG(extack,
1138 					       "Current ingress or clsact Qdisc has ongoing filter requests");
1139 				return -EBUSY;
1140 			}
1141 		}
1142 
1143 		if (dev->flags & IFF_UP)
1144 			dev_deactivate(dev, false);
1145 
1146 		qdisc_offload_graft_root(dev, new, old, extack);
1147 
1148 		if (new && new->ops->attach && !ingress)
1149 			goto skip;
1150 
1151 		if (!ingress) {
1152 			for (i = 0; i < num_q; i++) {
1153 				dev_queue = netdev_get_tx_queue(dev, i);
1154 				old = dev_graft_qdisc(dev_queue, new);
1155 
1156 				if (new && i > 0)
1157 					qdisc_refcount_inc(new);
1158 				qdisc_put(old);
1159 			}
1160 		} else {
1161 			old = dev_graft_qdisc(dev_queue, NULL);
1162 
1163 			/* {ingress,clsact}_destroy() @old before grafting @new to avoid
1164 			 * unprotected concurrent accesses to net_device::miniq_{in,e}gress
1165 			 * pointer(s) in mini_qdisc_pair_swap().
1166 			 */
1167 			qdisc_notify(net, skb, n, classid, old, new, extack);
1168 			qdisc_destroy(old);
1169 
1170 			dev_graft_qdisc(dev_queue, new);
1171 		}
1172 
1173 skip:
1174 		if (!ingress) {
1175 			old = rtnl_dereference(dev->qdisc);
1176 			if (new && !new->ops->attach)
1177 				qdisc_refcount_inc(new);
1178 			rcu_assign_pointer(dev->qdisc, new ? : &noop_qdisc);
1179 
1180 			notify_and_destroy(net, skb, n, classid, old, new, extack);
1181 
1182 			if (new && new->ops->attach)
1183 				new->ops->attach(new);
1184 		}
1185 
1186 		if (dev->flags & IFF_UP)
1187 			dev_activate(dev);
1188 	} else {
1189 		const struct Qdisc_class_ops *cops = parent->ops->cl_ops;
1190 		unsigned long cl;
1191 		int err;
1192 
1193 		/* Only support running class lockless if parent is lockless */
1194 		if (new && (new->flags & TCQ_F_NOLOCK) && !(parent->flags & TCQ_F_NOLOCK))
1195 			qdisc_clear_nolock(new);
1196 
1197 		if (!cops || !cops->graft)
1198 			return -EOPNOTSUPP;
1199 
1200 		cl = cops->find(parent, classid);
1201 		if (!cl) {
1202 			NL_SET_ERR_MSG(extack, "Specified class not found");
1203 			return -ENOENT;
1204 		}
1205 
1206 		if (new && new->ops == &noqueue_qdisc_ops) {
1207 			NL_SET_ERR_MSG(extack, "Cannot assign noqueue to a class");
1208 			return -EINVAL;
1209 		}
1210 
1211 		if (new &&
1212 		    !(parent->flags & TCQ_F_MQROOT) &&
1213 		    rcu_access_pointer(new->stab)) {
1214 			NL_SET_ERR_MSG(extack, "STAB not supported on a non root");
1215 			return -EINVAL;
1216 		}
1217 		if (new && parent->depth >= 7) {
1218 			NL_SET_ERR_MSG(extack, "Qdisc hierarchy is too deep");
1219 			return -E2BIG;
1220 		}
1221 		err = cops->graft(parent, cl, new, &old, extack);
1222 		if (err)
1223 			return err;
1224 		if (new)
1225 			new->depth = parent->depth + 1;
1226 		notify_and_destroy(net, skb, n, classid, old, new, extack);
1227 	}
1228 	return 0;
1229 }
1230 
1231 static int qdisc_block_indexes_set(struct Qdisc *sch, struct nlattr **tca,
1232 				   struct netlink_ext_ack *extack)
1233 {
1234 	u32 block_index;
1235 
1236 	if (tca[TCA_INGRESS_BLOCK]) {
1237 		block_index = nla_get_u32(tca[TCA_INGRESS_BLOCK]);
1238 
1239 		if (!block_index) {
1240 			NL_SET_ERR_MSG(extack, "Ingress block index cannot be 0");
1241 			return -EINVAL;
1242 		}
1243 		if (!sch->ops->ingress_block_set) {
1244 			NL_SET_ERR_MSG(extack, "Ingress block sharing is not supported");
1245 			return -EOPNOTSUPP;
1246 		}
1247 		sch->ops->ingress_block_set(sch, block_index);
1248 	}
1249 	if (tca[TCA_EGRESS_BLOCK]) {
1250 		block_index = nla_get_u32(tca[TCA_EGRESS_BLOCK]);
1251 
1252 		if (!block_index) {
1253 			NL_SET_ERR_MSG(extack, "Egress block index cannot be 0");
1254 			return -EINVAL;
1255 		}
1256 		if (!sch->ops->egress_block_set) {
1257 			NL_SET_ERR_MSG(extack, "Egress block sharing is not supported");
1258 			return -EOPNOTSUPP;
1259 		}
1260 		sch->ops->egress_block_set(sch, block_index);
1261 	}
1262 	return 0;
1263 }
1264 
1265 /*
1266    Allocate and initialize new qdisc.
1267 
1268    Parameters are passed via opt.
1269  */
1270 
1271 static struct Qdisc *qdisc_create(struct net_device *dev,
1272 				  struct netdev_queue *dev_queue,
1273 				  u32 parent, u32 handle,
1274 				  struct nlattr **tca, int *errp,
1275 				  struct netlink_ext_ack *extack)
1276 {
1277 	int err;
1278 	struct nlattr *kind = tca[TCA_KIND];
1279 	struct Qdisc *sch;
1280 	struct Qdisc_ops *ops;
1281 	struct qdisc_size_table *stab;
1282 
1283 	ops = qdisc_lookup_ops(kind);
1284 	if (!ops) {
1285 		err = -ENOENT;
1286 		NL_SET_ERR_MSG(extack, "Specified qdisc kind is unknown");
1287 		goto err_out;
1288 	}
1289 
1290 	sch = qdisc_alloc(dev_queue, ops, extack);
1291 	if (IS_ERR(sch)) {
1292 		err = PTR_ERR(sch);
1293 		goto err_out2;
1294 	}
1295 
1296 	sch->parent = parent;
1297 
1298 	if (handle == TC_H_INGRESS) {
1299 		if (!(sch->flags & TCQ_F_INGRESS)) {
1300 			NL_SET_ERR_MSG(extack,
1301 				       "Specified parent ID is reserved for ingress and clsact Qdiscs");
1302 			err = -EINVAL;
1303 			goto err_out3;
1304 		}
1305 		handle = TC_H_MAKE(TC_H_INGRESS, 0);
1306 	} else {
1307 		if (handle == 0) {
1308 			handle = qdisc_alloc_handle(dev);
1309 			if (handle == 0) {
1310 				NL_SET_ERR_MSG(extack, "Maximum number of qdisc handles was exceeded");
1311 				err = -ENOSPC;
1312 				goto err_out3;
1313 			}
1314 		}
1315 		if (!netif_is_multiqueue(dev))
1316 			sch->flags |= TCQ_F_ONETXQUEUE;
1317 	}
1318 
1319 	sch->handle = handle;
1320 
1321 	/* This exist to keep backward compatible with a userspace
1322 	 * loophole, what allowed userspace to get IFF_NO_QUEUE
1323 	 * facility on older kernels by setting tx_queue_len=0 (prior
1324 	 * to qdisc init), and then forgot to reinit tx_queue_len
1325 	 * before again attaching a qdisc.
1326 	 */
1327 	if ((dev->priv_flags & IFF_NO_QUEUE) && (dev->tx_queue_len == 0)) {
1328 		WRITE_ONCE(dev->tx_queue_len, DEFAULT_TX_QUEUE_LEN);
1329 		netdev_info(dev, "Caught tx_queue_len zero misconfig\n");
1330 	}
1331 
1332 	err = qdisc_block_indexes_set(sch, tca, extack);
1333 	if (err)
1334 		goto err_out3;
1335 
1336 	if (tca[TCA_STAB]) {
1337 		stab = qdisc_get_stab(tca[TCA_STAB], extack);
1338 		if (IS_ERR(stab)) {
1339 			err = PTR_ERR(stab);
1340 			goto err_out3;
1341 		}
1342 		rcu_assign_pointer(sch->stab, stab);
1343 	}
1344 
1345 	if (ops->init) {
1346 		err = ops->init(sch, tca[TCA_OPTIONS], extack);
1347 		if (err != 0)
1348 			goto err_out4;
1349 	}
1350 
1351 	if (tca[TCA_RATE]) {
1352 		err = -EOPNOTSUPP;
1353 		if (sch->flags & TCQ_F_MQROOT) {
1354 			NL_SET_ERR_MSG(extack, "Cannot attach rate estimator to a multi-queue root qdisc");
1355 			goto err_out4;
1356 		}
1357 
1358 		err = gen_new_estimator(&sch->bstats,
1359 					sch->cpu_bstats,
1360 					&sch->rate_est,
1361 					NULL,
1362 					true,
1363 					tca[TCA_RATE]);
1364 		if (err) {
1365 			NL_SET_ERR_MSG(extack, "Failed to generate new estimator");
1366 			goto err_out4;
1367 		}
1368 	}
1369 
1370 	qdisc_hash_add(sch, false);
1371 	trace_qdisc_create(ops, dev, parent);
1372 
1373 	return sch;
1374 
1375 err_out4:
1376 	/* Even if ops->init() failed, we call ops->destroy()
1377 	 * like qdisc_create_dflt().
1378 	 */
1379 	if (ops->destroy)
1380 		ops->destroy(sch);
1381 	qdisc_put_stab(rtnl_dereference(sch->stab));
1382 err_out3:
1383 	qdisc_lock_uninit(sch, ops);
1384 	netdev_put(dev, &sch->dev_tracker);
1385 	qdisc_free(sch);
1386 err_out2:
1387 	bpf_module_put(ops, ops->owner);
1388 err_out:
1389 	*errp = err;
1390 	return NULL;
1391 }
1392 
1393 static int qdisc_change(struct Qdisc *sch, struct nlattr **tca,
1394 			struct netlink_ext_ack *extack)
1395 {
1396 	struct qdisc_size_table *ostab, *stab = NULL;
1397 	int err = 0;
1398 
1399 	if (tca[TCA_OPTIONS]) {
1400 		if (!sch->ops->change) {
1401 			NL_SET_ERR_MSG(extack, "Change operation not supported by specified qdisc");
1402 			return -EINVAL;
1403 		}
1404 		if (tca[TCA_INGRESS_BLOCK] || tca[TCA_EGRESS_BLOCK]) {
1405 			NL_SET_ERR_MSG(extack, "Change of blocks is not supported");
1406 			return -EOPNOTSUPP;
1407 		}
1408 		err = sch->ops->change(sch, tca[TCA_OPTIONS], extack);
1409 		if (err)
1410 			return err;
1411 	}
1412 
1413 	if (tca[TCA_STAB]) {
1414 		stab = qdisc_get_stab(tca[TCA_STAB], extack);
1415 		if (IS_ERR(stab))
1416 			return PTR_ERR(stab);
1417 	}
1418 
1419 	ostab = rtnl_dereference(sch->stab);
1420 	rcu_assign_pointer(sch->stab, stab);
1421 	qdisc_put_stab(ostab);
1422 
1423 	if (tca[TCA_RATE]) {
1424 		/* NB: ignores errors from replace_estimator
1425 		   because change can't be undone. */
1426 		if (sch->flags & TCQ_F_MQROOT)
1427 			goto out;
1428 		gen_replace_estimator(&sch->bstats,
1429 				      sch->cpu_bstats,
1430 				      &sch->rate_est,
1431 				      NULL,
1432 				      true,
1433 				      tca[TCA_RATE]);
1434 	}
1435 out:
1436 	return 0;
1437 }
1438 
1439 struct check_loop_arg {
1440 	struct qdisc_walker	w;
1441 	struct Qdisc		*p;
1442 	int			depth;
1443 };
1444 
1445 static int check_loop_fn(struct Qdisc *q, unsigned long cl,
1446 			 struct qdisc_walker *w);
1447 
1448 static int check_loop(struct Qdisc *q, struct Qdisc *p, int depth)
1449 {
1450 	struct check_loop_arg	arg;
1451 
1452 	if (q->ops->cl_ops == NULL)
1453 		return 0;
1454 
1455 	arg.w.stop = arg.w.skip = arg.w.count = 0;
1456 	arg.w.fn = check_loop_fn;
1457 	arg.depth = depth;
1458 	arg.p = p;
1459 	q->ops->cl_ops->walk(q, &arg.w);
1460 	return arg.w.stop ? -ELOOP : 0;
1461 }
1462 
1463 static int
1464 check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w)
1465 {
1466 	struct Qdisc *leaf;
1467 	const struct Qdisc_class_ops *cops = q->ops->cl_ops;
1468 	struct check_loop_arg *arg = (struct check_loop_arg *)w;
1469 
1470 	leaf = cops->leaf(q, cl);
1471 	if (leaf) {
1472 		if (leaf == arg->p || arg->depth > 7)
1473 			return -ELOOP;
1474 		return check_loop(leaf, arg->p, arg->depth + 1);
1475 	}
1476 	return 0;
1477 }
1478 
1479 const struct nla_policy rtm_tca_policy[TCA_MAX + 1] = {
1480 	[TCA_KIND]		= { .type = NLA_STRING },
1481 	[TCA_RATE]		= { .type = NLA_BINARY,
1482 				    .len = sizeof(struct tc_estimator) },
1483 	[TCA_STAB]		= { .type = NLA_NESTED },
1484 	[TCA_DUMP_INVISIBLE]	= { .type = NLA_FLAG },
1485 	[TCA_CHAIN]		= { .type = NLA_U32 },
1486 	[TCA_INGRESS_BLOCK]	= { .type = NLA_U32 },
1487 	[TCA_EGRESS_BLOCK]	= { .type = NLA_U32 },
1488 };
1489 
1490 /*
1491  * Delete/get qdisc.
1492  */
1493 
1494 static int __tc_get_qdisc(struct sk_buff *skb, struct nlmsghdr *n,
1495 			  struct netlink_ext_ack *extack,
1496 			  struct net_device *dev,
1497 			  struct nlattr *tca[TCA_MAX + 1],
1498 			  struct tcmsg *tcm)
1499 {
1500 	struct net *net = sock_net(skb->sk);
1501 	struct Qdisc *q = NULL;
1502 	struct Qdisc *p = NULL;
1503 	u32 clid;
1504 	int err;
1505 
1506 	clid = tcm->tcm_parent;
1507 	if (clid) {
1508 		if (clid != TC_H_ROOT) {
1509 			if (TC_H_MAJ(clid) != TC_H_MAJ(TC_H_INGRESS)) {
1510 				p = qdisc_lookup(dev, TC_H_MAJ(clid));
1511 				if (!p) {
1512 					NL_SET_ERR_MSG(extack, "Failed to find qdisc with specified classid");
1513 					return -ENOENT;
1514 				}
1515 				q = qdisc_leaf(p, clid, extack);
1516 			} else if (dev_ingress_queue(dev)) {
1517 				q = rtnl_dereference(dev_ingress_queue(dev)->qdisc_sleeping);
1518 			}
1519 		} else {
1520 			q = rtnl_dereference(dev->qdisc);
1521 		}
1522 		if (!q) {
1523 			NL_SET_ERR_MSG(extack, "Cannot find specified qdisc on specified device");
1524 			return -ENOENT;
1525 		}
1526 		if (IS_ERR(q))
1527 			return PTR_ERR(q);
1528 
1529 		if (tcm->tcm_handle && q->handle != tcm->tcm_handle) {
1530 			NL_SET_ERR_MSG(extack, "Invalid handle");
1531 			return -EINVAL;
1532 		}
1533 	} else {
1534 		q = qdisc_lookup(dev, tcm->tcm_handle);
1535 		if (!q) {
1536 			NL_SET_ERR_MSG(extack, "Failed to find qdisc with specified handle");
1537 			return -ENOENT;
1538 		}
1539 	}
1540 
1541 	if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id)) {
1542 		NL_SET_ERR_MSG(extack, "Invalid qdisc name: must match existing qdisc");
1543 		return -EINVAL;
1544 	}
1545 
1546 	if (n->nlmsg_type == RTM_DELQDISC) {
1547 		if (!clid) {
1548 			NL_SET_ERR_MSG(extack, "Classid cannot be zero");
1549 			return -EINVAL;
1550 		}
1551 		if (q->handle == 0) {
1552 			NL_SET_ERR_MSG(extack, "Cannot delete qdisc with handle of zero");
1553 			return -ENOENT;
1554 		}
1555 		err = qdisc_graft(dev, p, skb, n, clid, NULL, q, extack);
1556 		if (err != 0)
1557 			return err;
1558 	} else {
1559 		qdisc_get_notify(net, skb, n, clid, q, NULL);
1560 	}
1561 	return 0;
1562 }
1563 
1564 static int tc_get_qdisc(struct sk_buff *skb, struct nlmsghdr *n,
1565 			struct netlink_ext_ack *extack)
1566 {
1567 	struct net *net = sock_net(skb->sk);
1568 	struct tcmsg *tcm = nlmsg_data(n);
1569 	struct nlattr *tca[TCA_MAX + 1];
1570 	struct net_device *dev;
1571 	int err;
1572 
1573 	err = nlmsg_parse_deprecated(n, sizeof(*tcm), tca, TCA_MAX,
1574 				     rtm_tca_policy, extack);
1575 	if (err < 0)
1576 		return err;
1577 
1578 	dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1579 	if (!dev)
1580 		return -ENODEV;
1581 
1582 	netdev_lock_ops(dev);
1583 	err = __tc_get_qdisc(skb, n, extack, dev, tca, tcm);
1584 	netdev_unlock_ops(dev);
1585 
1586 	return err;
1587 }
1588 
1589 static bool req_create_or_replace(struct nlmsghdr *n)
1590 {
1591 	return (n->nlmsg_flags & NLM_F_CREATE &&
1592 		n->nlmsg_flags & NLM_F_REPLACE);
1593 }
1594 
1595 static bool req_create_exclusive(struct nlmsghdr *n)
1596 {
1597 	return (n->nlmsg_flags & NLM_F_CREATE &&
1598 		n->nlmsg_flags & NLM_F_EXCL);
1599 }
1600 
1601 static bool req_change(struct nlmsghdr *n)
1602 {
1603 	return (!(n->nlmsg_flags & NLM_F_CREATE) &&
1604 		!(n->nlmsg_flags & NLM_F_REPLACE) &&
1605 		!(n->nlmsg_flags & NLM_F_EXCL));
1606 }
1607 
1608 static int __tc_modify_qdisc(struct sk_buff *skb, struct nlmsghdr *n,
1609 			     struct netlink_ext_ack *extack,
1610 			     struct net_device *dev,
1611 			     struct nlattr *tca[TCA_MAX + 1],
1612 			     struct tcmsg *tcm)
1613 {
1614 	struct Qdisc *q = NULL;
1615 	struct Qdisc *p = NULL;
1616 	u32 clid;
1617 	int err;
1618 
1619 	clid = tcm->tcm_parent;
1620 
1621 	if (clid) {
1622 		if (clid != TC_H_ROOT) {
1623 			if (clid != TC_H_INGRESS) {
1624 				p = qdisc_lookup(dev, TC_H_MAJ(clid));
1625 				if (!p) {
1626 					NL_SET_ERR_MSG(extack, "Failed to find specified qdisc");
1627 					return -ENOENT;
1628 				}
1629 				if (p->flags & TCQ_F_INGRESS) {
1630 					NL_SET_ERR_MSG(extack,
1631 						       "Cannot add children to ingress/clsact qdisc");
1632 					return -EOPNOTSUPP;
1633 				}
1634 				q = qdisc_leaf(p, clid, extack);
1635 				if (IS_ERR(q))
1636 					return PTR_ERR(q);
1637 			} else if (dev_ingress_queue_create(dev)) {
1638 				q = rtnl_dereference(dev_ingress_queue(dev)->qdisc_sleeping);
1639 			}
1640 		} else {
1641 			q = rtnl_dereference(dev->qdisc);
1642 		}
1643 
1644 		/* It may be default qdisc, ignore it */
1645 		if (q && q->handle == 0)
1646 			q = NULL;
1647 
1648 		if (!q || !tcm->tcm_handle || q->handle != tcm->tcm_handle) {
1649 			if (tcm->tcm_handle) {
1650 				if (q && !(n->nlmsg_flags & NLM_F_REPLACE)) {
1651 					NL_SET_ERR_MSG(extack, "NLM_F_REPLACE needed to override");
1652 					return -EEXIST;
1653 				}
1654 				if (TC_H_MIN(tcm->tcm_handle)) {
1655 					NL_SET_ERR_MSG(extack, "Invalid minor handle");
1656 					return -EINVAL;
1657 				}
1658 				q = qdisc_lookup(dev, tcm->tcm_handle);
1659 				if (!q)
1660 					goto create_n_graft;
1661 				if (q->parent != tcm->tcm_parent) {
1662 					NL_SET_ERR_MSG(extack, "Cannot move an existing qdisc to a different parent");
1663 					return -EINVAL;
1664 				}
1665 				if (n->nlmsg_flags & NLM_F_EXCL) {
1666 					NL_SET_ERR_MSG(extack, "Exclusivity flag on, cannot override");
1667 					return -EEXIST;
1668 				}
1669 				if (tca[TCA_KIND] &&
1670 				    nla_strcmp(tca[TCA_KIND], q->ops->id)) {
1671 					NL_SET_ERR_MSG(extack, "Invalid qdisc name: must match existing qdisc");
1672 					return -EINVAL;
1673 				}
1674 				if (q->flags & TCQ_F_INGRESS) {
1675 					NL_SET_ERR_MSG(extack,
1676 						       "Cannot regraft ingress or clsact Qdiscs");
1677 					return -EINVAL;
1678 				}
1679 				if (q == p ||
1680 				    (p && check_loop(q, p, 0))) {
1681 					NL_SET_ERR_MSG(extack, "Qdisc parent/child loop detected");
1682 					return -ELOOP;
1683 				}
1684 				if (clid == TC_H_INGRESS) {
1685 					NL_SET_ERR_MSG(extack, "Ingress cannot graft directly");
1686 					return -EINVAL;
1687 				}
1688 				qdisc_refcount_inc(q);
1689 				goto graft;
1690 			} else {
1691 				if (!q)
1692 					goto create_n_graft;
1693 
1694 				/* This magic test requires explanation.
1695 				 *
1696 				 *   We know, that some child q is already
1697 				 *   attached to this parent and have choice:
1698 				 *   1) change it or 2) create/graft new one.
1699 				 *   If the requested qdisc kind is different
1700 				 *   than the existing one, then we choose graft.
1701 				 *   If they are the same then this is "change"
1702 				 *   operation - just let it fallthrough..
1703 				 *
1704 				 *   1. We are allowed to create/graft only
1705 				 *   if the request is explicitly stating
1706 				 *   "please create if it doesn't exist".
1707 				 *
1708 				 *   2. If the request is to exclusive create
1709 				 *   then the qdisc tcm_handle is not expected
1710 				 *   to exist, so that we choose create/graft too.
1711 				 *
1712 				 *   3. The last case is when no flags are set.
1713 				 *   This will happen when for example tc
1714 				 *   utility issues a "change" command.
1715 				 *   Alas, it is sort of hole in API, we
1716 				 *   cannot decide what to do unambiguously.
1717 				 *   For now we select create/graft.
1718 				 */
1719 				if (tca[TCA_KIND] &&
1720 				    nla_strcmp(tca[TCA_KIND], q->ops->id)) {
1721 					if (req_create_or_replace(n) ||
1722 					    req_create_exclusive(n))
1723 						goto create_n_graft;
1724 					else if (req_change(n))
1725 						goto create_n_graft2;
1726 				}
1727 			}
1728 		}
1729 	} else {
1730 		if (!tcm->tcm_handle) {
1731 			NL_SET_ERR_MSG(extack, "Handle cannot be zero");
1732 			return -EINVAL;
1733 		}
1734 		q = qdisc_lookup(dev, tcm->tcm_handle);
1735 	}
1736 
1737 	/* Change qdisc parameters */
1738 	if (!q) {
1739 		NL_SET_ERR_MSG(extack, "Specified qdisc not found");
1740 		return -ENOENT;
1741 	}
1742 	if (n->nlmsg_flags & NLM_F_EXCL) {
1743 		NL_SET_ERR_MSG(extack, "Exclusivity flag on, cannot modify");
1744 		return -EEXIST;
1745 	}
1746 	if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id)) {
1747 		NL_SET_ERR_MSG(extack, "Invalid qdisc name: must match existing qdisc");
1748 		return -EINVAL;
1749 	}
1750 	err = qdisc_change(q, tca, extack);
1751 	if (err == 0)
1752 		qdisc_notify(sock_net(skb->sk), skb, n, clid, NULL, q, extack);
1753 	return err;
1754 
1755 create_n_graft:
1756 	if (!(n->nlmsg_flags & NLM_F_CREATE)) {
1757 		NL_SET_ERR_MSG(extack, "Qdisc not found. To create specify NLM_F_CREATE flag");
1758 		return -ENOENT;
1759 	}
1760 create_n_graft2:
1761 	if (clid == TC_H_INGRESS) {
1762 		if (dev_ingress_queue(dev)) {
1763 			q = qdisc_create(dev, dev_ingress_queue(dev),
1764 					 tcm->tcm_parent, tcm->tcm_parent,
1765 					 tca, &err, extack);
1766 		} else {
1767 			NL_SET_ERR_MSG(extack, "Cannot find ingress queue for specified device");
1768 			err = -ENOENT;
1769 		}
1770 	} else {
1771 		struct netdev_queue *dev_queue;
1772 
1773 		if (p && p->ops->cl_ops && p->ops->cl_ops->select_queue)
1774 			dev_queue = p->ops->cl_ops->select_queue(p, tcm);
1775 		else if (p)
1776 			dev_queue = p->dev_queue;
1777 		else
1778 			dev_queue = netdev_get_tx_queue(dev, 0);
1779 
1780 		q = qdisc_create(dev, dev_queue,
1781 				 tcm->tcm_parent, tcm->tcm_handle,
1782 				 tca, &err, extack);
1783 	}
1784 	if (!q)
1785 		return err;
1786 
1787 graft:
1788 	err = qdisc_graft(dev, p, skb, n, clid, q, NULL, extack);
1789 	if (err) {
1790 		if (q)
1791 			qdisc_put(q);
1792 		return err;
1793 	}
1794 
1795 	return 0;
1796 }
1797 
1798 static void request_qdisc_module(struct nlattr *kind)
1799 {
1800 	struct Qdisc_ops *ops;
1801 	char name[IFNAMSIZ];
1802 
1803 	if (!kind)
1804 		return;
1805 
1806 	ops = qdisc_lookup_ops(kind);
1807 	if (ops) {
1808 		bpf_module_put(ops, ops->owner);
1809 		return;
1810 	}
1811 
1812 	if (nla_strscpy(name, kind, IFNAMSIZ) >= 0) {
1813 		rtnl_unlock();
1814 		request_module(NET_SCH_ALIAS_PREFIX "%s", name);
1815 		rtnl_lock();
1816 	}
1817 }
1818 
1819 /*
1820  * Create/change qdisc.
1821  */
1822 static int tc_modify_qdisc(struct sk_buff *skb, struct nlmsghdr *n,
1823 			   struct netlink_ext_ack *extack)
1824 {
1825 	struct net *net = sock_net(skb->sk);
1826 	struct nlattr *tca[TCA_MAX + 1];
1827 	struct net_device *dev;
1828 	struct tcmsg *tcm;
1829 	int err;
1830 
1831 	err = nlmsg_parse_deprecated(n, sizeof(*tcm), tca, TCA_MAX,
1832 				     rtm_tca_policy, extack);
1833 	if (err < 0)
1834 		return err;
1835 
1836 	request_qdisc_module(tca[TCA_KIND]);
1837 
1838 	tcm = nlmsg_data(n);
1839 	dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1840 	if (!dev)
1841 		return -ENODEV;
1842 
1843 	netdev_lock_ops(dev);
1844 	err = __tc_modify_qdisc(skb, n, extack, dev, tca, tcm);
1845 	netdev_unlock_ops(dev);
1846 
1847 	return err;
1848 }
1849 
1850 static int tc_dump_qdisc_root(struct Qdisc *root, struct sk_buff *skb,
1851 			      struct netlink_callback *cb,
1852 			      int *q_idx_p, int s_q_idx, bool recur,
1853 			      bool dump_invisible)
1854 {
1855 	const struct nlmsghdr *nlh = cb->nlh;
1856 	int ret = 0, q_idx = *q_idx_p;
1857 	const struct tcmsg *tcm;
1858 	struct Qdisc *q;
1859 	int b;
1860 
1861 	if (!root)
1862 		return 0;
1863 
1864 	tcm = nlmsg_data(nlh);
1865 	q = root;
1866 	if (q_idx < s_q_idx) {
1867 		q_idx++;
1868 	} else {
1869 		if (!tc_qdisc_dump_ignore(q, dump_invisible, tcm))
1870 		    ret = tc_fill_qdisc(skb, q, q->parent,
1871 					NETLINK_CB(cb->skb).portid,
1872 					nlh->nlmsg_seq, NLM_F_MULTI,
1873 					RTM_NEWQDISC, NULL);
1874 		if (ret < 0)
1875 			goto out;
1876 		q_idx++;
1877 	}
1878 
1879 	/* If dumping singletons, there is no qdisc_dev(root) and the singleton
1880 	 * itself has already been dumped.
1881 	 *
1882 	 * If we've already dumped the top-level (ingress) qdisc above and the global
1883 	 * qdisc hashtable, we don't want to hit it again
1884 	 */
1885 	if (!qdisc_dev(root) || !recur)
1886 		goto out;
1887 
1888 	hash_for_each(qdisc_dev(root)->qdisc_hash, b, q, hash) {
1889 		if (q_idx < s_q_idx) {
1890 			q_idx++;
1891 			continue;
1892 		}
1893 		if (!tc_qdisc_dump_ignore(q, dump_invisible, tcm))
1894 			ret = tc_fill_qdisc(skb, q, q->parent,
1895 					    NETLINK_CB(cb->skb).portid,
1896 					    nlh->nlmsg_seq, NLM_F_MULTI,
1897 					    RTM_NEWQDISC, NULL);
1898 		if (ret < 0)
1899 			goto out;
1900 		q_idx++;
1901 	}
1902 
1903 out:
1904 	*q_idx_p = q_idx;
1905 	return ret;
1906 }
1907 
1908 static int tc_dump_qdisc(struct sk_buff *skb, struct netlink_callback *cb)
1909 {
1910 	const struct nlmsghdr *nlh = cb->nlh;
1911 	struct net *net = sock_net(skb->sk);
1912 	struct nlattr *tca[TCA_MAX + 1];
1913 	struct {
1914 		unsigned long ifindex;
1915 		int q_idx;
1916 	} *ctx = (void *)cb->ctx;
1917 	const struct tcmsg *tcm;
1918 	struct net_device *dev;
1919 	int s_q_idx, q_idx;
1920 	int err;
1921 
1922 	ASSERT_RTNL();
1923 
1924 	err = nlmsg_parse_deprecated(nlh, sizeof(struct tcmsg), tca, TCA_MAX,
1925 				     rtm_tca_policy, cb->extack);
1926 	if (err < 0)
1927 		return err;
1928 	tcm = nlmsg_data(nlh);
1929 	if (tcm->tcm_ifindex && !ctx->ifindex)
1930 		ctx->ifindex = tcm->tcm_ifindex;
1931 
1932 	s_q_idx = ctx->q_idx;
1933 
1934 	for_each_netdev_dump(net, dev, ctx->ifindex) {
1935 		struct netdev_queue *dev_queue;
1936 		struct Qdisc *q;
1937 
1938 		if (tcm->tcm_ifindex && ctx->ifindex != tcm->tcm_ifindex)
1939 			break;
1940 
1941 		q_idx = 0;
1942 
1943 		netdev_lock_ops(dev);
1944 		q = rtnl_dereference(dev->qdisc);
1945 		err = tc_dump_qdisc_root(q, skb, cb, &q_idx, s_q_idx,
1946 					 true, tca[TCA_DUMP_INVISIBLE]);
1947 		if (err < 0)
1948 			goto error_unlock;
1949 
1950 		dev_queue = dev_ingress_queue(dev);
1951 		if (dev_queue) {
1952 			q = rtnl_dereference(dev_queue->qdisc_sleeping);
1953 			err = tc_dump_qdisc_root(q, skb, cb, &q_idx, s_q_idx,
1954 						 false, tca[TCA_DUMP_INVISIBLE]);
1955 			if (err < 0)
1956 				goto error_unlock;
1957 		}
1958 		netdev_unlock_ops(dev);
1959 		s_q_idx = 0;
1960 	}
1961 	return skb->len;
1962 
1963 error_unlock:
1964 	netdev_unlock_ops(dev);
1965 	ctx->q_idx = q_idx;
1966 
1967 	return err;
1968 }
1969 
1970 
1971 
1972 /************************************************
1973  *	Traffic classes manipulation.		*
1974  ************************************************/
1975 
1976 static int tc_fill_tclass(struct sk_buff *skb, struct Qdisc *q,
1977 			  unsigned long cl, u32 portid, u32 seq, u16 flags,
1978 			  int event, struct netlink_ext_ack *extack)
1979 {
1980 	struct tcmsg *tcm;
1981 	struct nlmsghdr  *nlh;
1982 	unsigned char *b = skb_tail_pointer(skb);
1983 	struct gnet_dump d;
1984 	const struct Qdisc_class_ops *cl_ops = q->ops->cl_ops;
1985 
1986 	cond_resched();
1987 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1988 	if (!nlh)
1989 		goto out_nlmsg_trim;
1990 	tcm = nlmsg_data(nlh);
1991 	tcm->tcm_family = AF_UNSPEC;
1992 	tcm->tcm__pad1 = 0;
1993 	tcm->tcm__pad2 = 0;
1994 	tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1995 	tcm->tcm_parent = q->handle;
1996 	tcm->tcm_handle = q->handle;
1997 	tcm->tcm_info = 0;
1998 	if (nla_put_string(skb, TCA_KIND, q->ops->id))
1999 		goto nla_put_failure;
2000 	if (cl_ops->dump && cl_ops->dump(q, cl, skb, tcm) < 0)
2001 		goto nla_put_failure;
2002 
2003 	if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS, TCA_XSTATS,
2004 					 NULL, &d, TCA_PAD) < 0)
2005 		goto nla_put_failure;
2006 
2007 	if (cl_ops->dump_stats && cl_ops->dump_stats(q, cl, &d) < 0)
2008 		goto nla_put_failure;
2009 
2010 	if (gnet_stats_finish_copy(&d) < 0)
2011 		goto nla_put_failure;
2012 
2013 	if (extack && extack->_msg &&
2014 	    nla_put_string(skb, TCA_EXT_WARN_MSG, extack->_msg))
2015 		goto out_nlmsg_trim;
2016 
2017 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
2018 
2019 	return skb->len;
2020 
2021 out_nlmsg_trim:
2022 nla_put_failure:
2023 	nlmsg_trim(skb, b);
2024 	return -EMSGSIZE;
2025 }
2026 
2027 static int tclass_notify(struct net *net, struct sk_buff *oskb,
2028 			 struct nlmsghdr *n, struct Qdisc *q,
2029 			 unsigned long cl, int event, struct netlink_ext_ack *extack)
2030 {
2031 	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2032 	struct sk_buff *skb;
2033 	int ret;
2034 
2035 	if (!rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC))
2036 		return 0;
2037 
2038 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2039 	if (!skb)
2040 		return -ENOBUFS;
2041 
2042 	ret = tc_fill_tclass(skb, q, cl, portid, n->nlmsg_seq, 0, event, extack);
2043 	if (ret < 0) {
2044 		kfree_skb(skb);
2045 		return ret;
2046 	}
2047 
2048 	return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
2049 			      n->nlmsg_flags & NLM_F_ECHO);
2050 }
2051 
2052 static int tclass_get_notify(struct net *net, struct sk_buff *oskb,
2053 			     struct nlmsghdr *n, struct Qdisc *q,
2054 			     unsigned long cl, struct netlink_ext_ack *extack)
2055 {
2056 	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2057 	struct sk_buff *skb;
2058 	int ret;
2059 
2060 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2061 	if (!skb)
2062 		return -ENOBUFS;
2063 
2064 	ret = tc_fill_tclass(skb, q, cl, portid, n->nlmsg_seq, 0,
2065 			     RTM_NEWTCLASS, extack);
2066 	if (ret < 0) {
2067 		kfree_skb(skb);
2068 		return ret;
2069 	}
2070 
2071 	return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
2072 			      n->nlmsg_flags & NLM_F_ECHO);
2073 }
2074 
2075 static int tclass_del_notify(struct net *net,
2076 			     const struct Qdisc_class_ops *cops,
2077 			     struct sk_buff *oskb, struct nlmsghdr *n,
2078 			     struct Qdisc *q, unsigned long cl,
2079 			     struct netlink_ext_ack *extack)
2080 {
2081 	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2082 	struct sk_buff *skb = NULL;
2083 	int err = 0;
2084 
2085 	if (!cops->delete)
2086 		return -EOPNOTSUPP;
2087 
2088 	if (rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC)) {
2089 		skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2090 		if (!skb)
2091 			return -ENOBUFS;
2092 
2093 		err = tc_fill_tclass(skb, q, cl, portid, n->nlmsg_seq, 0,
2094 				     RTM_DELTCLASS, extack);
2095 		if (err < 0) {
2096 			kfree_skb(skb);
2097 			return err;
2098 		}
2099 	}
2100 
2101 	err = cops->delete(q, cl, extack);
2102 	if (err) {
2103 		kfree_skb(skb);
2104 		return err;
2105 	}
2106 
2107 	err = rtnetlink_maybe_send(skb, net, portid, RTNLGRP_TC,
2108 				   n->nlmsg_flags & NLM_F_ECHO);
2109 	return err;
2110 }
2111 
2112 #ifdef CONFIG_NET_CLS
2113 
2114 struct tcf_bind_args {
2115 	struct tcf_walker w;
2116 	unsigned long base;
2117 	unsigned long cl;
2118 	u32 classid;
2119 };
2120 
2121 static int tcf_node_bind(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
2122 {
2123 	struct tcf_bind_args *a = (void *)arg;
2124 
2125 	if (n && tp->ops->bind_class) {
2126 		struct Qdisc *q = tcf_block_q(tp->chain->block);
2127 
2128 		sch_tree_lock(q);
2129 		tp->ops->bind_class(n, a->classid, a->cl, q, a->base);
2130 		sch_tree_unlock(q);
2131 	}
2132 	return 0;
2133 }
2134 
2135 struct tc_bind_class_args {
2136 	struct qdisc_walker w;
2137 	unsigned long new_cl;
2138 	u32 portid;
2139 	u32 clid;
2140 };
2141 
2142 static int tc_bind_class_walker(struct Qdisc *q, unsigned long cl,
2143 				struct qdisc_walker *w)
2144 {
2145 	struct tc_bind_class_args *a = (struct tc_bind_class_args *)w;
2146 	const struct Qdisc_class_ops *cops = q->ops->cl_ops;
2147 	struct tcf_block *block;
2148 	struct tcf_chain *chain;
2149 
2150 	block = cops->tcf_block(q, cl, NULL);
2151 	if (!block)
2152 		return 0;
2153 	for (chain = tcf_get_next_chain(block, NULL);
2154 	     chain;
2155 	     chain = tcf_get_next_chain(block, chain)) {
2156 		struct tcf_proto *tp;
2157 
2158 		for (tp = tcf_get_next_proto(chain, NULL);
2159 		     tp; tp = tcf_get_next_proto(chain, tp)) {
2160 			struct tcf_bind_args arg = {};
2161 
2162 			arg.w.fn = tcf_node_bind;
2163 			arg.classid = a->clid;
2164 			arg.base = cl;
2165 			arg.cl = a->new_cl;
2166 			tp->ops->walk(tp, &arg.w, true);
2167 		}
2168 	}
2169 
2170 	return 0;
2171 }
2172 
2173 static void tc_bind_tclass(struct Qdisc *q, u32 portid, u32 clid,
2174 			   unsigned long new_cl)
2175 {
2176 	const struct Qdisc_class_ops *cops = q->ops->cl_ops;
2177 	struct tc_bind_class_args args = {};
2178 
2179 	if (!cops->tcf_block)
2180 		return;
2181 	args.portid = portid;
2182 	args.clid = clid;
2183 	args.new_cl = new_cl;
2184 	args.w.fn = tc_bind_class_walker;
2185 	q->ops->cl_ops->walk(q, &args.w);
2186 }
2187 
2188 #else
2189 
2190 static void tc_bind_tclass(struct Qdisc *q, u32 portid, u32 clid,
2191 			   unsigned long new_cl)
2192 {
2193 }
2194 
2195 #endif
2196 
2197 static int __tc_ctl_tclass(struct sk_buff *skb, struct nlmsghdr *n,
2198 			   struct netlink_ext_ack *extack,
2199 			   struct net_device *dev,
2200 			   struct nlattr *tca[TCA_MAX + 1],
2201 			   struct tcmsg *tcm)
2202 {
2203 	struct net *net = sock_net(skb->sk);
2204 	const struct Qdisc_class_ops *cops;
2205 	struct Qdisc *q = NULL;
2206 	unsigned long cl = 0;
2207 	unsigned long new_cl;
2208 	u32 portid;
2209 	u32 clid;
2210 	u32 qid;
2211 	int err;
2212 
2213 	/*
2214 	   parent == TC_H_UNSPEC - unspecified parent.
2215 	   parent == TC_H_ROOT   - class is root, which has no parent.
2216 	   parent == X:0	 - parent is root class.
2217 	   parent == X:Y	 - parent is a node in hierarchy.
2218 	   parent == 0:Y	 - parent is X:Y, where X:0 is qdisc.
2219 
2220 	   handle == 0:0	 - generate handle from kernel pool.
2221 	   handle == 0:Y	 - class is X:Y, where X:0 is qdisc.
2222 	   handle == X:Y	 - clear.
2223 	   handle == X:0	 - root class.
2224 	 */
2225 
2226 	/* Step 1. Determine qdisc handle X:0 */
2227 
2228 	portid = tcm->tcm_parent;
2229 	clid = tcm->tcm_handle;
2230 	qid = TC_H_MAJ(clid);
2231 
2232 	if (portid != TC_H_ROOT) {
2233 		u32 qid1 = TC_H_MAJ(portid);
2234 
2235 		if (qid && qid1) {
2236 			/* If both majors are known, they must be identical. */
2237 			if (qid != qid1)
2238 				return -EINVAL;
2239 		} else if (qid1) {
2240 			qid = qid1;
2241 		} else if (qid == 0)
2242 			qid = rtnl_dereference(dev->qdisc)->handle;
2243 
2244 		/* Now qid is genuine qdisc handle consistent
2245 		 * both with parent and child.
2246 		 *
2247 		 * TC_H_MAJ(portid) still may be unspecified, complete it now.
2248 		 */
2249 		if (portid)
2250 			portid = TC_H_MAKE(qid, portid);
2251 	} else {
2252 		if (qid == 0)
2253 			qid = rtnl_dereference(dev->qdisc)->handle;
2254 	}
2255 
2256 	/* OK. Locate qdisc */
2257 	q = qdisc_lookup(dev, qid);
2258 	if (!q)
2259 		return -ENOENT;
2260 
2261 	/* An check that it supports classes */
2262 	cops = q->ops->cl_ops;
2263 	if (cops == NULL)
2264 		return -EINVAL;
2265 
2266 	/* Now try to get class */
2267 	if (clid == 0) {
2268 		if (portid == TC_H_ROOT)
2269 			clid = qid;
2270 	} else
2271 		clid = TC_H_MAKE(qid, clid);
2272 
2273 	if (clid)
2274 		cl = cops->find(q, clid);
2275 
2276 	if (cl == 0) {
2277 		err = -ENOENT;
2278 		if (n->nlmsg_type != RTM_NEWTCLASS ||
2279 		    !(n->nlmsg_flags & NLM_F_CREATE))
2280 			goto out;
2281 	} else {
2282 		switch (n->nlmsg_type) {
2283 		case RTM_NEWTCLASS:
2284 			err = -EEXIST;
2285 			if (n->nlmsg_flags & NLM_F_EXCL)
2286 				goto out;
2287 			break;
2288 		case RTM_DELTCLASS:
2289 			err = tclass_del_notify(net, cops, skb, n, q, cl, extack);
2290 			/* Unbind the class with flilters with 0 */
2291 			tc_bind_tclass(q, portid, clid, 0);
2292 			goto out;
2293 		case RTM_GETTCLASS:
2294 			err = tclass_get_notify(net, skb, n, q, cl, extack);
2295 			goto out;
2296 		default:
2297 			err = -EINVAL;
2298 			goto out;
2299 		}
2300 	}
2301 
2302 	if (tca[TCA_INGRESS_BLOCK] || tca[TCA_EGRESS_BLOCK]) {
2303 		NL_SET_ERR_MSG(extack, "Shared blocks are not supported for classes");
2304 		return -EOPNOTSUPP;
2305 	}
2306 
2307 	/* Prevent creation of traffic classes with classid TC_H_ROOT */
2308 	if (clid == TC_H_ROOT) {
2309 		NL_SET_ERR_MSG(extack, "Cannot create traffic class with classid TC_H_ROOT");
2310 		return -EINVAL;
2311 	}
2312 
2313 	new_cl = cl;
2314 	err = -EOPNOTSUPP;
2315 	if (cops->change)
2316 		err = cops->change(q, clid, portid, tca, &new_cl, extack);
2317 	if (err == 0) {
2318 		tclass_notify(net, skb, n, q, new_cl, RTM_NEWTCLASS, extack);
2319 		/* We just create a new class, need to do reverse binding. */
2320 		if (cl != new_cl)
2321 			tc_bind_tclass(q, portid, clid, new_cl);
2322 	}
2323 out:
2324 	return err;
2325 }
2326 
2327 static int tc_ctl_tclass(struct sk_buff *skb, struct nlmsghdr *n,
2328 			 struct netlink_ext_ack *extack)
2329 {
2330 	struct net *net = sock_net(skb->sk);
2331 	struct tcmsg *tcm = nlmsg_data(n);
2332 	struct nlattr *tca[TCA_MAX + 1];
2333 	struct net_device *dev;
2334 	int err;
2335 
2336 	err = nlmsg_parse_deprecated(n, sizeof(*tcm), tca, TCA_MAX,
2337 				     rtm_tca_policy, extack);
2338 	if (err < 0)
2339 		return err;
2340 
2341 	dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2342 	if (!dev)
2343 		return -ENODEV;
2344 
2345 	netdev_lock_ops(dev);
2346 	err = __tc_ctl_tclass(skb, n, extack, dev, tca, tcm);
2347 	netdev_unlock_ops(dev);
2348 
2349 	return err;
2350 }
2351 
2352 struct qdisc_dump_args {
2353 	struct qdisc_walker	w;
2354 	struct sk_buff		*skb;
2355 	struct netlink_callback	*cb;
2356 };
2357 
2358 static int qdisc_class_dump(struct Qdisc *q, unsigned long cl,
2359 			    struct qdisc_walker *arg)
2360 {
2361 	struct qdisc_dump_args *a = (struct qdisc_dump_args *)arg;
2362 
2363 	return tc_fill_tclass(a->skb, q, cl, NETLINK_CB(a->cb->skb).portid,
2364 			      a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
2365 			      RTM_NEWTCLASS, NULL);
2366 }
2367 
2368 static int tc_dump_tclass_qdisc(struct Qdisc *q, struct sk_buff *skb,
2369 				struct tcmsg *tcm, struct netlink_callback *cb,
2370 				int *t_p, int s_t)
2371 {
2372 	struct qdisc_dump_args arg;
2373 
2374 	if (tc_qdisc_dump_ignore(q, false, NULL) ||
2375 	    *t_p < s_t || !q->ops->cl_ops ||
2376 	    (tcm->tcm_parent &&
2377 	     TC_H_MAJ(tcm->tcm_parent) != q->handle)) {
2378 		(*t_p)++;
2379 		return 0;
2380 	}
2381 	if (*t_p > s_t)
2382 		memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
2383 	arg.w.fn = qdisc_class_dump;
2384 	arg.skb = skb;
2385 	arg.cb = cb;
2386 	arg.w.stop  = 0;
2387 	arg.w.skip = cb->args[1];
2388 	arg.w.count = 0;
2389 	q->ops->cl_ops->walk(q, &arg.w);
2390 	cb->args[1] = arg.w.count;
2391 	if (arg.w.stop)
2392 		return -1;
2393 	(*t_p)++;
2394 	return 0;
2395 }
2396 
2397 static int tc_dump_tclass_root(struct Qdisc *root, struct sk_buff *skb,
2398 			       struct tcmsg *tcm, struct netlink_callback *cb,
2399 			       int *t_p, int s_t, bool recur)
2400 {
2401 	struct Qdisc *q;
2402 	int b;
2403 
2404 	if (!root)
2405 		return 0;
2406 
2407 	if (tc_dump_tclass_qdisc(root, skb, tcm, cb, t_p, s_t) < 0)
2408 		return -1;
2409 
2410 	if (!qdisc_dev(root) || !recur)
2411 		return 0;
2412 
2413 	if (tcm->tcm_parent) {
2414 		q = qdisc_match_from_root(root, TC_H_MAJ(tcm->tcm_parent));
2415 		if (q && q != root &&
2416 		    tc_dump_tclass_qdisc(q, skb, tcm, cb, t_p, s_t) < 0)
2417 			return -1;
2418 		return 0;
2419 	}
2420 	hash_for_each(qdisc_dev(root)->qdisc_hash, b, q, hash) {
2421 		if (tc_dump_tclass_qdisc(q, skb, tcm, cb, t_p, s_t) < 0)
2422 			return -1;
2423 	}
2424 
2425 	return 0;
2426 }
2427 
2428 static int __tc_dump_tclass(struct sk_buff *skb, struct netlink_callback *cb,
2429 			    struct tcmsg *tcm, struct net_device *dev)
2430 {
2431 	struct netdev_queue *dev_queue;
2432 	int t, s_t;
2433 
2434 	s_t = cb->args[0];
2435 	t = 0;
2436 
2437 	if (tc_dump_tclass_root(rtnl_dereference(dev->qdisc),
2438 				skb, tcm, cb, &t, s_t, true) < 0)
2439 		goto done;
2440 
2441 	dev_queue = dev_ingress_queue(dev);
2442 	if (dev_queue &&
2443 	    tc_dump_tclass_root(rtnl_dereference(dev_queue->qdisc_sleeping),
2444 				skb, tcm, cb, &t, s_t, false) < 0)
2445 		goto done;
2446 
2447 done:
2448 	cb->args[0] = t;
2449 
2450 	return skb->len;
2451 }
2452 
2453 static int tc_dump_tclass(struct sk_buff *skb, struct netlink_callback *cb)
2454 {
2455 	struct tcmsg *tcm = nlmsg_data(cb->nlh);
2456 	struct net *net = sock_net(skb->sk);
2457 	struct net_device *dev;
2458 	int err;
2459 
2460 	if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2461 		return 0;
2462 
2463 	dev = dev_get_by_index(net, tcm->tcm_ifindex);
2464 	if (!dev)
2465 		return 0;
2466 
2467 	netdev_lock_ops(dev);
2468 	err = __tc_dump_tclass(skb, cb, tcm, dev);
2469 	netdev_unlock_ops(dev);
2470 
2471 	dev_put(dev);
2472 
2473 	return err;
2474 }
2475 
2476 #ifdef CONFIG_PROC_FS
2477 static int psched_show(struct seq_file *seq, void *v)
2478 {
2479 	seq_printf(seq, "%08x %08x %08x %08x\n",
2480 		   (u32)NSEC_PER_USEC, (u32)PSCHED_TICKS2NS(1),
2481 		   1000000,
2482 		   (u32)NSEC_PER_SEC / hrtimer_resolution);
2483 
2484 	return 0;
2485 }
2486 
2487 static int __net_init psched_net_init(struct net *net)
2488 {
2489 	struct proc_dir_entry *e;
2490 
2491 	e = proc_create_single("psched", 0, net->proc_net, psched_show);
2492 	if (e == NULL)
2493 		return -ENOMEM;
2494 
2495 	return 0;
2496 }
2497 
2498 static void __net_exit psched_net_exit(struct net *net)
2499 {
2500 	remove_proc_entry("psched", net->proc_net);
2501 }
2502 #else
2503 static int __net_init psched_net_init(struct net *net)
2504 {
2505 	return 0;
2506 }
2507 
2508 static void __net_exit psched_net_exit(struct net *net)
2509 {
2510 }
2511 #endif
2512 
2513 static struct pernet_operations psched_net_ops = {
2514 	.init = psched_net_init,
2515 	.exit = psched_net_exit,
2516 };
2517 
2518 #if IS_ENABLED(CONFIG_MITIGATION_RETPOLINE)
2519 DEFINE_STATIC_KEY_FALSE(tc_skip_wrapper_act);
2520 DEFINE_STATIC_KEY_FALSE(tc_skip_wrapper_cls);
2521 #endif
2522 
2523 static const struct rtnl_msg_handler psched_rtnl_msg_handlers[] __initconst = {
2524 	{.msgtype = RTM_NEWQDISC, .doit = tc_modify_qdisc},
2525 	{.msgtype = RTM_DELQDISC, .doit = tc_get_qdisc},
2526 	{.msgtype = RTM_GETQDISC, .doit = tc_get_qdisc,
2527 	 .dumpit = tc_dump_qdisc},
2528 	{.msgtype = RTM_NEWTCLASS, .doit = tc_ctl_tclass},
2529 	{.msgtype = RTM_DELTCLASS, .doit = tc_ctl_tclass},
2530 	{.msgtype = RTM_GETTCLASS, .doit = tc_ctl_tclass,
2531 	 .dumpit = tc_dump_tclass},
2532 };
2533 
2534 static int __init pktsched_init(void)
2535 {
2536 	int err;
2537 
2538 	err = register_pernet_subsys(&psched_net_ops);
2539 	if (err) {
2540 		pr_err("pktsched_init: "
2541 		       "cannot initialize per netns operations\n");
2542 		return err;
2543 	}
2544 
2545 	register_qdisc(&pfifo_fast_ops);
2546 	register_qdisc(&pfifo_qdisc_ops);
2547 	register_qdisc(&bfifo_qdisc_ops);
2548 	register_qdisc(&pfifo_head_drop_qdisc_ops);
2549 	register_qdisc(&mq_qdisc_ops);
2550 	register_qdisc(&noqueue_qdisc_ops);
2551 
2552 	rtnl_register_many(psched_rtnl_msg_handlers);
2553 
2554 	tc_wrapper_init();
2555 
2556 	return 0;
2557 }
2558 
2559 subsys_initcall(pktsched_init);
2560