xref: /linux/net/sched/sch_api.c (revision 5c458073553f0ef74f5c8db1bd459c87c722a299)
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 		ingress = 0;
1118 		num_q = dev->num_tx_queues;
1119 		if ((q && q->flags & TCQ_F_INGRESS) ||
1120 		    (new && new->flags & TCQ_F_INGRESS)) {
1121 			ingress = 1;
1122 			dev_queue = dev_ingress_queue(dev);
1123 			if (!dev_queue) {
1124 				NL_SET_ERR_MSG(extack, "Device does not have an ingress queue");
1125 				return -ENOENT;
1126 			}
1127 
1128 			q = rtnl_dereference(dev_queue->qdisc_sleeping);
1129 
1130 			/* This is the counterpart of that qdisc_refcount_inc_nz() call in
1131 			 * __tcf_qdisc_find() for filter requests.
1132 			 */
1133 			if (!qdisc_refcount_dec_if_one(q)) {
1134 				NL_SET_ERR_MSG(extack,
1135 					       "Current ingress or clsact Qdisc has ongoing filter requests");
1136 				return -EBUSY;
1137 			}
1138 		}
1139 
1140 		if (dev->flags & IFF_UP)
1141 			dev_deactivate(dev, false);
1142 
1143 		qdisc_offload_graft_root(dev, new, old, extack);
1144 
1145 		if (new && new->ops->attach && !ingress)
1146 			goto skip;
1147 
1148 		if (!ingress) {
1149 			for (i = 0; i < num_q; i++) {
1150 				dev_queue = netdev_get_tx_queue(dev, i);
1151 				old = dev_graft_qdisc(dev_queue, new);
1152 
1153 				if (new && i > 0)
1154 					qdisc_refcount_inc(new);
1155 				qdisc_put(old);
1156 			}
1157 		} else {
1158 			old = dev_graft_qdisc(dev_queue, NULL);
1159 
1160 			/* {ingress,clsact}_destroy() @old before grafting @new to avoid
1161 			 * unprotected concurrent accesses to net_device::miniq_{in,e}gress
1162 			 * pointer(s) in mini_qdisc_pair_swap().
1163 			 */
1164 			qdisc_notify(net, skb, n, classid, old, new, extack);
1165 			qdisc_destroy(old);
1166 
1167 			dev_graft_qdisc(dev_queue, new);
1168 		}
1169 
1170 skip:
1171 		if (!ingress) {
1172 			old = rtnl_dereference(dev->qdisc);
1173 			if (new && !new->ops->attach)
1174 				qdisc_refcount_inc(new);
1175 			rcu_assign_pointer(dev->qdisc, new ? : &noop_qdisc);
1176 
1177 			notify_and_destroy(net, skb, n, classid, old, new, extack);
1178 
1179 			if (new && new->ops->attach)
1180 				new->ops->attach(new);
1181 		}
1182 
1183 		if (dev->flags & IFF_UP)
1184 			dev_activate(dev);
1185 	} else {
1186 		const struct Qdisc_class_ops *cops = parent->ops->cl_ops;
1187 		unsigned long cl;
1188 		int err;
1189 
1190 		/* Only support running class lockless if parent is lockless */
1191 		if (new && (new->flags & TCQ_F_NOLOCK) && !(parent->flags & TCQ_F_NOLOCK))
1192 			qdisc_clear_nolock(new);
1193 
1194 		if (!cops || !cops->graft)
1195 			return -EOPNOTSUPP;
1196 
1197 		cl = cops->find(parent, classid);
1198 		if (!cl) {
1199 			NL_SET_ERR_MSG(extack, "Specified class not found");
1200 			return -ENOENT;
1201 		}
1202 
1203 		if (new && new->ops == &noqueue_qdisc_ops) {
1204 			NL_SET_ERR_MSG(extack, "Cannot assign noqueue to a class");
1205 			return -EINVAL;
1206 		}
1207 
1208 		if (new &&
1209 		    !(parent->flags & TCQ_F_MQROOT) &&
1210 		    rcu_access_pointer(new->stab)) {
1211 			NL_SET_ERR_MSG(extack, "STAB not supported on a non root");
1212 			return -EINVAL;
1213 		}
1214 		err = cops->graft(parent, cl, new, &old, extack);
1215 		if (err)
1216 			return err;
1217 		notify_and_destroy(net, skb, n, classid, old, new, extack);
1218 	}
1219 	return 0;
1220 }
1221 
1222 static int qdisc_block_indexes_set(struct Qdisc *sch, struct nlattr **tca,
1223 				   struct netlink_ext_ack *extack)
1224 {
1225 	u32 block_index;
1226 
1227 	if (tca[TCA_INGRESS_BLOCK]) {
1228 		block_index = nla_get_u32(tca[TCA_INGRESS_BLOCK]);
1229 
1230 		if (!block_index) {
1231 			NL_SET_ERR_MSG(extack, "Ingress block index cannot be 0");
1232 			return -EINVAL;
1233 		}
1234 		if (!sch->ops->ingress_block_set) {
1235 			NL_SET_ERR_MSG(extack, "Ingress block sharing is not supported");
1236 			return -EOPNOTSUPP;
1237 		}
1238 		sch->ops->ingress_block_set(sch, block_index);
1239 	}
1240 	if (tca[TCA_EGRESS_BLOCK]) {
1241 		block_index = nla_get_u32(tca[TCA_EGRESS_BLOCK]);
1242 
1243 		if (!block_index) {
1244 			NL_SET_ERR_MSG(extack, "Egress block index cannot be 0");
1245 			return -EINVAL;
1246 		}
1247 		if (!sch->ops->egress_block_set) {
1248 			NL_SET_ERR_MSG(extack, "Egress block sharing is not supported");
1249 			return -EOPNOTSUPP;
1250 		}
1251 		sch->ops->egress_block_set(sch, block_index);
1252 	}
1253 	return 0;
1254 }
1255 
1256 /*
1257    Allocate and initialize new qdisc.
1258 
1259    Parameters are passed via opt.
1260  */
1261 
1262 static struct Qdisc *qdisc_create(struct net_device *dev,
1263 				  struct netdev_queue *dev_queue,
1264 				  u32 parent, u32 handle,
1265 				  struct nlattr **tca, int *errp,
1266 				  struct netlink_ext_ack *extack)
1267 {
1268 	int err;
1269 	struct nlattr *kind = tca[TCA_KIND];
1270 	struct Qdisc *sch;
1271 	struct Qdisc_ops *ops;
1272 	struct qdisc_size_table *stab;
1273 
1274 	ops = qdisc_lookup_ops(kind);
1275 	if (!ops) {
1276 		err = -ENOENT;
1277 		NL_SET_ERR_MSG(extack, "Specified qdisc kind is unknown");
1278 		goto err_out;
1279 	}
1280 
1281 	sch = qdisc_alloc(dev_queue, ops, extack);
1282 	if (IS_ERR(sch)) {
1283 		err = PTR_ERR(sch);
1284 		goto err_out2;
1285 	}
1286 
1287 	sch->parent = parent;
1288 
1289 	if (handle == TC_H_INGRESS) {
1290 		if (!(sch->flags & TCQ_F_INGRESS)) {
1291 			NL_SET_ERR_MSG(extack,
1292 				       "Specified parent ID is reserved for ingress and clsact Qdiscs");
1293 			err = -EINVAL;
1294 			goto err_out3;
1295 		}
1296 		handle = TC_H_MAKE(TC_H_INGRESS, 0);
1297 	} else {
1298 		if (handle == 0) {
1299 			handle = qdisc_alloc_handle(dev);
1300 			if (handle == 0) {
1301 				NL_SET_ERR_MSG(extack, "Maximum number of qdisc handles was exceeded");
1302 				err = -ENOSPC;
1303 				goto err_out3;
1304 			}
1305 		}
1306 		if (!netif_is_multiqueue(dev))
1307 			sch->flags |= TCQ_F_ONETXQUEUE;
1308 	}
1309 
1310 	sch->handle = handle;
1311 
1312 	/* This exist to keep backward compatible with a userspace
1313 	 * loophole, what allowed userspace to get IFF_NO_QUEUE
1314 	 * facility on older kernels by setting tx_queue_len=0 (prior
1315 	 * to qdisc init), and then forgot to reinit tx_queue_len
1316 	 * before again attaching a qdisc.
1317 	 */
1318 	if ((dev->priv_flags & IFF_NO_QUEUE) && (dev->tx_queue_len == 0)) {
1319 		WRITE_ONCE(dev->tx_queue_len, DEFAULT_TX_QUEUE_LEN);
1320 		netdev_info(dev, "Caught tx_queue_len zero misconfig\n");
1321 	}
1322 
1323 	err = qdisc_block_indexes_set(sch, tca, extack);
1324 	if (err)
1325 		goto err_out3;
1326 
1327 	if (tca[TCA_STAB]) {
1328 		stab = qdisc_get_stab(tca[TCA_STAB], extack);
1329 		if (IS_ERR(stab)) {
1330 			err = PTR_ERR(stab);
1331 			goto err_out3;
1332 		}
1333 		rcu_assign_pointer(sch->stab, stab);
1334 	}
1335 
1336 	if (ops->init) {
1337 		err = ops->init(sch, tca[TCA_OPTIONS], extack);
1338 		if (err != 0)
1339 			goto err_out4;
1340 	}
1341 
1342 	if (tca[TCA_RATE]) {
1343 		err = -EOPNOTSUPP;
1344 		if (sch->flags & TCQ_F_MQROOT) {
1345 			NL_SET_ERR_MSG(extack, "Cannot attach rate estimator to a multi-queue root qdisc");
1346 			goto err_out4;
1347 		}
1348 
1349 		err = gen_new_estimator(&sch->bstats,
1350 					sch->cpu_bstats,
1351 					&sch->rate_est,
1352 					NULL,
1353 					true,
1354 					tca[TCA_RATE]);
1355 		if (err) {
1356 			NL_SET_ERR_MSG(extack, "Failed to generate new estimator");
1357 			goto err_out4;
1358 		}
1359 	}
1360 
1361 	qdisc_hash_add(sch, false);
1362 	trace_qdisc_create(ops, dev, parent);
1363 
1364 	return sch;
1365 
1366 err_out4:
1367 	/* Even if ops->init() failed, we call ops->destroy()
1368 	 * like qdisc_create_dflt().
1369 	 */
1370 	if (ops->destroy)
1371 		ops->destroy(sch);
1372 	qdisc_put_stab(rtnl_dereference(sch->stab));
1373 err_out3:
1374 	qdisc_lock_uninit(sch, ops);
1375 	netdev_put(dev, &sch->dev_tracker);
1376 	qdisc_free(sch);
1377 err_out2:
1378 	bpf_module_put(ops, ops->owner);
1379 err_out:
1380 	*errp = err;
1381 	return NULL;
1382 }
1383 
1384 static int qdisc_change(struct Qdisc *sch, struct nlattr **tca,
1385 			struct netlink_ext_ack *extack)
1386 {
1387 	struct qdisc_size_table *ostab, *stab = NULL;
1388 	int err = 0;
1389 
1390 	if (tca[TCA_OPTIONS]) {
1391 		if (!sch->ops->change) {
1392 			NL_SET_ERR_MSG(extack, "Change operation not supported by specified qdisc");
1393 			return -EINVAL;
1394 		}
1395 		if (tca[TCA_INGRESS_BLOCK] || tca[TCA_EGRESS_BLOCK]) {
1396 			NL_SET_ERR_MSG(extack, "Change of blocks is not supported");
1397 			return -EOPNOTSUPP;
1398 		}
1399 		err = sch->ops->change(sch, tca[TCA_OPTIONS], extack);
1400 		if (err)
1401 			return err;
1402 	}
1403 
1404 	if (tca[TCA_STAB]) {
1405 		stab = qdisc_get_stab(tca[TCA_STAB], extack);
1406 		if (IS_ERR(stab))
1407 			return PTR_ERR(stab);
1408 	}
1409 
1410 	ostab = rtnl_dereference(sch->stab);
1411 	rcu_assign_pointer(sch->stab, stab);
1412 	qdisc_put_stab(ostab);
1413 
1414 	if (tca[TCA_RATE]) {
1415 		/* NB: ignores errors from replace_estimator
1416 		   because change can't be undone. */
1417 		if (sch->flags & TCQ_F_MQROOT)
1418 			goto out;
1419 		gen_replace_estimator(&sch->bstats,
1420 				      sch->cpu_bstats,
1421 				      &sch->rate_est,
1422 				      NULL,
1423 				      true,
1424 				      tca[TCA_RATE]);
1425 	}
1426 out:
1427 	return 0;
1428 }
1429 
1430 struct check_loop_arg {
1431 	struct qdisc_walker	w;
1432 	struct Qdisc		*p;
1433 	int			depth;
1434 };
1435 
1436 static int check_loop_fn(struct Qdisc *q, unsigned long cl,
1437 			 struct qdisc_walker *w);
1438 
1439 static int check_loop(struct Qdisc *q, struct Qdisc *p, int depth)
1440 {
1441 	struct check_loop_arg	arg;
1442 
1443 	if (q->ops->cl_ops == NULL)
1444 		return 0;
1445 
1446 	arg.w.stop = arg.w.skip = arg.w.count = 0;
1447 	arg.w.fn = check_loop_fn;
1448 	arg.depth = depth;
1449 	arg.p = p;
1450 	q->ops->cl_ops->walk(q, &arg.w);
1451 	return arg.w.stop ? -ELOOP : 0;
1452 }
1453 
1454 static int
1455 check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w)
1456 {
1457 	struct Qdisc *leaf;
1458 	const struct Qdisc_class_ops *cops = q->ops->cl_ops;
1459 	struct check_loop_arg *arg = (struct check_loop_arg *)w;
1460 
1461 	leaf = cops->leaf(q, cl);
1462 	if (leaf) {
1463 		if (leaf == arg->p || arg->depth > 7)
1464 			return -ELOOP;
1465 		return check_loop(leaf, arg->p, arg->depth + 1);
1466 	}
1467 	return 0;
1468 }
1469 
1470 const struct nla_policy rtm_tca_policy[TCA_MAX + 1] = {
1471 	[TCA_KIND]		= { .type = NLA_STRING },
1472 	[TCA_RATE]		= { .type = NLA_BINARY,
1473 				    .len = sizeof(struct tc_estimator) },
1474 	[TCA_STAB]		= { .type = NLA_NESTED },
1475 	[TCA_DUMP_INVISIBLE]	= { .type = NLA_FLAG },
1476 	[TCA_CHAIN]		= { .type = NLA_U32 },
1477 	[TCA_INGRESS_BLOCK]	= { .type = NLA_U32 },
1478 	[TCA_EGRESS_BLOCK]	= { .type = NLA_U32 },
1479 };
1480 
1481 /*
1482  * Delete/get qdisc.
1483  */
1484 
1485 static int __tc_get_qdisc(struct sk_buff *skb, struct nlmsghdr *n,
1486 			  struct netlink_ext_ack *extack,
1487 			  struct net_device *dev,
1488 			  struct nlattr *tca[TCA_MAX + 1],
1489 			  struct tcmsg *tcm)
1490 {
1491 	struct net *net = sock_net(skb->sk);
1492 	struct Qdisc *q = NULL;
1493 	struct Qdisc *p = NULL;
1494 	u32 clid;
1495 	int err;
1496 
1497 	clid = tcm->tcm_parent;
1498 	if (clid) {
1499 		if (clid != TC_H_ROOT) {
1500 			if (TC_H_MAJ(clid) != TC_H_MAJ(TC_H_INGRESS)) {
1501 				p = qdisc_lookup(dev, TC_H_MAJ(clid));
1502 				if (!p) {
1503 					NL_SET_ERR_MSG(extack, "Failed to find qdisc with specified classid");
1504 					return -ENOENT;
1505 				}
1506 				q = qdisc_leaf(p, clid, extack);
1507 			} else if (dev_ingress_queue(dev)) {
1508 				q = rtnl_dereference(dev_ingress_queue(dev)->qdisc_sleeping);
1509 			}
1510 		} else {
1511 			q = rtnl_dereference(dev->qdisc);
1512 		}
1513 		if (!q) {
1514 			NL_SET_ERR_MSG(extack, "Cannot find specified qdisc on specified device");
1515 			return -ENOENT;
1516 		}
1517 		if (IS_ERR(q))
1518 			return PTR_ERR(q);
1519 
1520 		if (tcm->tcm_handle && q->handle != tcm->tcm_handle) {
1521 			NL_SET_ERR_MSG(extack, "Invalid handle");
1522 			return -EINVAL;
1523 		}
1524 	} else {
1525 		q = qdisc_lookup(dev, tcm->tcm_handle);
1526 		if (!q) {
1527 			NL_SET_ERR_MSG(extack, "Failed to find qdisc with specified handle");
1528 			return -ENOENT;
1529 		}
1530 	}
1531 
1532 	if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id)) {
1533 		NL_SET_ERR_MSG(extack, "Invalid qdisc name: must match existing qdisc");
1534 		return -EINVAL;
1535 	}
1536 
1537 	if (n->nlmsg_type == RTM_DELQDISC) {
1538 		if (!clid) {
1539 			NL_SET_ERR_MSG(extack, "Classid cannot be zero");
1540 			return -EINVAL;
1541 		}
1542 		if (q->handle == 0) {
1543 			NL_SET_ERR_MSG(extack, "Cannot delete qdisc with handle of zero");
1544 			return -ENOENT;
1545 		}
1546 		err = qdisc_graft(dev, p, skb, n, clid, NULL, q, extack);
1547 		if (err != 0)
1548 			return err;
1549 	} else {
1550 		qdisc_get_notify(net, skb, n, clid, q, NULL);
1551 	}
1552 	return 0;
1553 }
1554 
1555 static int tc_get_qdisc(struct sk_buff *skb, struct nlmsghdr *n,
1556 			struct netlink_ext_ack *extack)
1557 {
1558 	struct net *net = sock_net(skb->sk);
1559 	struct tcmsg *tcm = nlmsg_data(n);
1560 	struct nlattr *tca[TCA_MAX + 1];
1561 	struct net_device *dev;
1562 	int err;
1563 
1564 	err = nlmsg_parse_deprecated(n, sizeof(*tcm), tca, TCA_MAX,
1565 				     rtm_tca_policy, extack);
1566 	if (err < 0)
1567 		return err;
1568 
1569 	dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1570 	if (!dev)
1571 		return -ENODEV;
1572 
1573 	netdev_lock_ops(dev);
1574 	err = __tc_get_qdisc(skb, n, extack, dev, tca, tcm);
1575 	netdev_unlock_ops(dev);
1576 
1577 	return err;
1578 }
1579 
1580 static bool req_create_or_replace(struct nlmsghdr *n)
1581 {
1582 	return (n->nlmsg_flags & NLM_F_CREATE &&
1583 		n->nlmsg_flags & NLM_F_REPLACE);
1584 }
1585 
1586 static bool req_create_exclusive(struct nlmsghdr *n)
1587 {
1588 	return (n->nlmsg_flags & NLM_F_CREATE &&
1589 		n->nlmsg_flags & NLM_F_EXCL);
1590 }
1591 
1592 static bool req_change(struct nlmsghdr *n)
1593 {
1594 	return (!(n->nlmsg_flags & NLM_F_CREATE) &&
1595 		!(n->nlmsg_flags & NLM_F_REPLACE) &&
1596 		!(n->nlmsg_flags & NLM_F_EXCL));
1597 }
1598 
1599 static int __tc_modify_qdisc(struct sk_buff *skb, struct nlmsghdr *n,
1600 			     struct netlink_ext_ack *extack,
1601 			     struct net_device *dev,
1602 			     struct nlattr *tca[TCA_MAX + 1],
1603 			     struct tcmsg *tcm)
1604 {
1605 	struct Qdisc *q = NULL;
1606 	struct Qdisc *p = NULL;
1607 	u32 clid;
1608 	int err;
1609 
1610 	clid = tcm->tcm_parent;
1611 
1612 	if (clid) {
1613 		if (clid != TC_H_ROOT) {
1614 			if (clid != TC_H_INGRESS) {
1615 				p = qdisc_lookup(dev, TC_H_MAJ(clid));
1616 				if (!p) {
1617 					NL_SET_ERR_MSG(extack, "Failed to find specified qdisc");
1618 					return -ENOENT;
1619 				}
1620 				if (p->flags & TCQ_F_INGRESS) {
1621 					NL_SET_ERR_MSG(extack,
1622 						       "Cannot add children to ingress/clsact qdisc");
1623 					return -EOPNOTSUPP;
1624 				}
1625 				q = qdisc_leaf(p, clid, extack);
1626 				if (IS_ERR(q))
1627 					return PTR_ERR(q);
1628 			} else if (dev_ingress_queue_create(dev)) {
1629 				q = rtnl_dereference(dev_ingress_queue(dev)->qdisc_sleeping);
1630 			}
1631 		} else {
1632 			q = rtnl_dereference(dev->qdisc);
1633 		}
1634 
1635 		/* It may be default qdisc, ignore it */
1636 		if (q && q->handle == 0)
1637 			q = NULL;
1638 
1639 		if (!q || !tcm->tcm_handle || q->handle != tcm->tcm_handle) {
1640 			if (tcm->tcm_handle) {
1641 				if (q && !(n->nlmsg_flags & NLM_F_REPLACE)) {
1642 					NL_SET_ERR_MSG(extack, "NLM_F_REPLACE needed to override");
1643 					return -EEXIST;
1644 				}
1645 				if (TC_H_MIN(tcm->tcm_handle)) {
1646 					NL_SET_ERR_MSG(extack, "Invalid minor handle");
1647 					return -EINVAL;
1648 				}
1649 				q = qdisc_lookup(dev, tcm->tcm_handle);
1650 				if (!q)
1651 					goto create_n_graft;
1652 				if (q->parent != tcm->tcm_parent) {
1653 					NL_SET_ERR_MSG(extack, "Cannot move an existing qdisc to a different parent");
1654 					return -EINVAL;
1655 				}
1656 				if (n->nlmsg_flags & NLM_F_EXCL) {
1657 					NL_SET_ERR_MSG(extack, "Exclusivity flag on, cannot override");
1658 					return -EEXIST;
1659 				}
1660 				if (tca[TCA_KIND] &&
1661 				    nla_strcmp(tca[TCA_KIND], q->ops->id)) {
1662 					NL_SET_ERR_MSG(extack, "Invalid qdisc name: must match existing qdisc");
1663 					return -EINVAL;
1664 				}
1665 				if (q->flags & TCQ_F_INGRESS) {
1666 					NL_SET_ERR_MSG(extack,
1667 						       "Cannot regraft ingress or clsact Qdiscs");
1668 					return -EINVAL;
1669 				}
1670 				if (q == p ||
1671 				    (p && check_loop(q, p, 0))) {
1672 					NL_SET_ERR_MSG(extack, "Qdisc parent/child loop detected");
1673 					return -ELOOP;
1674 				}
1675 				if (clid == TC_H_INGRESS) {
1676 					NL_SET_ERR_MSG(extack, "Ingress cannot graft directly");
1677 					return -EINVAL;
1678 				}
1679 				qdisc_refcount_inc(q);
1680 				goto graft;
1681 			} else {
1682 				if (!q)
1683 					goto create_n_graft;
1684 
1685 				/* This magic test requires explanation.
1686 				 *
1687 				 *   We know, that some child q is already
1688 				 *   attached to this parent and have choice:
1689 				 *   1) change it or 2) create/graft new one.
1690 				 *   If the requested qdisc kind is different
1691 				 *   than the existing one, then we choose graft.
1692 				 *   If they are the same then this is "change"
1693 				 *   operation - just let it fallthrough..
1694 				 *
1695 				 *   1. We are allowed to create/graft only
1696 				 *   if the request is explicitly stating
1697 				 *   "please create if it doesn't exist".
1698 				 *
1699 				 *   2. If the request is to exclusive create
1700 				 *   then the qdisc tcm_handle is not expected
1701 				 *   to exist, so that we choose create/graft too.
1702 				 *
1703 				 *   3. The last case is when no flags are set.
1704 				 *   This will happen when for example tc
1705 				 *   utility issues a "change" command.
1706 				 *   Alas, it is sort of hole in API, we
1707 				 *   cannot decide what to do unambiguously.
1708 				 *   For now we select create/graft.
1709 				 */
1710 				if (tca[TCA_KIND] &&
1711 				    nla_strcmp(tca[TCA_KIND], q->ops->id)) {
1712 					if (req_create_or_replace(n) ||
1713 					    req_create_exclusive(n))
1714 						goto create_n_graft;
1715 					else if (req_change(n))
1716 						goto create_n_graft2;
1717 				}
1718 			}
1719 		}
1720 	} else {
1721 		if (!tcm->tcm_handle) {
1722 			NL_SET_ERR_MSG(extack, "Handle cannot be zero");
1723 			return -EINVAL;
1724 		}
1725 		q = qdisc_lookup(dev, tcm->tcm_handle);
1726 	}
1727 
1728 	/* Change qdisc parameters */
1729 	if (!q) {
1730 		NL_SET_ERR_MSG(extack, "Specified qdisc not found");
1731 		return -ENOENT;
1732 	}
1733 	if (n->nlmsg_flags & NLM_F_EXCL) {
1734 		NL_SET_ERR_MSG(extack, "Exclusivity flag on, cannot modify");
1735 		return -EEXIST;
1736 	}
1737 	if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id)) {
1738 		NL_SET_ERR_MSG(extack, "Invalid qdisc name: must match existing qdisc");
1739 		return -EINVAL;
1740 	}
1741 	err = qdisc_change(q, tca, extack);
1742 	if (err == 0)
1743 		qdisc_notify(sock_net(skb->sk), skb, n, clid, NULL, q, extack);
1744 	return err;
1745 
1746 create_n_graft:
1747 	if (!(n->nlmsg_flags & NLM_F_CREATE)) {
1748 		NL_SET_ERR_MSG(extack, "Qdisc not found. To create specify NLM_F_CREATE flag");
1749 		return -ENOENT;
1750 	}
1751 create_n_graft2:
1752 	if (clid == TC_H_INGRESS) {
1753 		if (dev_ingress_queue(dev)) {
1754 			q = qdisc_create(dev, dev_ingress_queue(dev),
1755 					 tcm->tcm_parent, tcm->tcm_parent,
1756 					 tca, &err, extack);
1757 		} else {
1758 			NL_SET_ERR_MSG(extack, "Cannot find ingress queue for specified device");
1759 			err = -ENOENT;
1760 		}
1761 	} else {
1762 		struct netdev_queue *dev_queue;
1763 
1764 		if (p && p->ops->cl_ops && p->ops->cl_ops->select_queue)
1765 			dev_queue = p->ops->cl_ops->select_queue(p, tcm);
1766 		else if (p)
1767 			dev_queue = p->dev_queue;
1768 		else
1769 			dev_queue = netdev_get_tx_queue(dev, 0);
1770 
1771 		q = qdisc_create(dev, dev_queue,
1772 				 tcm->tcm_parent, tcm->tcm_handle,
1773 				 tca, &err, extack);
1774 	}
1775 	if (!q)
1776 		return err;
1777 
1778 graft:
1779 	err = qdisc_graft(dev, p, skb, n, clid, q, NULL, extack);
1780 	if (err) {
1781 		if (q)
1782 			qdisc_put(q);
1783 		return err;
1784 	}
1785 
1786 	return 0;
1787 }
1788 
1789 static void request_qdisc_module(struct nlattr *kind)
1790 {
1791 	struct Qdisc_ops *ops;
1792 	char name[IFNAMSIZ];
1793 
1794 	if (!kind)
1795 		return;
1796 
1797 	ops = qdisc_lookup_ops(kind);
1798 	if (ops) {
1799 		bpf_module_put(ops, ops->owner);
1800 		return;
1801 	}
1802 
1803 	if (nla_strscpy(name, kind, IFNAMSIZ) >= 0) {
1804 		rtnl_unlock();
1805 		request_module(NET_SCH_ALIAS_PREFIX "%s", name);
1806 		rtnl_lock();
1807 	}
1808 }
1809 
1810 /*
1811  * Create/change qdisc.
1812  */
1813 static int tc_modify_qdisc(struct sk_buff *skb, struct nlmsghdr *n,
1814 			   struct netlink_ext_ack *extack)
1815 {
1816 	struct net *net = sock_net(skb->sk);
1817 	struct nlattr *tca[TCA_MAX + 1];
1818 	struct net_device *dev;
1819 	struct tcmsg *tcm;
1820 	int err;
1821 
1822 	err = nlmsg_parse_deprecated(n, sizeof(*tcm), tca, TCA_MAX,
1823 				     rtm_tca_policy, extack);
1824 	if (err < 0)
1825 		return err;
1826 
1827 	request_qdisc_module(tca[TCA_KIND]);
1828 
1829 	tcm = nlmsg_data(n);
1830 	dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1831 	if (!dev)
1832 		return -ENODEV;
1833 
1834 	netdev_lock_ops(dev);
1835 	err = __tc_modify_qdisc(skb, n, extack, dev, tca, tcm);
1836 	netdev_unlock_ops(dev);
1837 
1838 	return err;
1839 }
1840 
1841 static int tc_dump_qdisc_root(struct Qdisc *root, struct sk_buff *skb,
1842 			      struct netlink_callback *cb,
1843 			      int *q_idx_p, int s_q_idx, bool recur,
1844 			      bool dump_invisible)
1845 {
1846 	const struct nlmsghdr *nlh = cb->nlh;
1847 	int ret = 0, q_idx = *q_idx_p;
1848 	const struct tcmsg *tcm;
1849 	struct Qdisc *q;
1850 	int b;
1851 
1852 	if (!root)
1853 		return 0;
1854 
1855 	tcm = nlmsg_data(nlh);
1856 	q = root;
1857 	if (q_idx < s_q_idx) {
1858 		q_idx++;
1859 	} else {
1860 		if (!tc_qdisc_dump_ignore(q, dump_invisible, tcm))
1861 		    ret = tc_fill_qdisc(skb, q, q->parent,
1862 					NETLINK_CB(cb->skb).portid,
1863 					nlh->nlmsg_seq, NLM_F_MULTI,
1864 					RTM_NEWQDISC, NULL);
1865 		if (ret < 0)
1866 			goto out;
1867 		q_idx++;
1868 	}
1869 
1870 	/* If dumping singletons, there is no qdisc_dev(root) and the singleton
1871 	 * itself has already been dumped.
1872 	 *
1873 	 * If we've already dumped the top-level (ingress) qdisc above and the global
1874 	 * qdisc hashtable, we don't want to hit it again
1875 	 */
1876 	if (!qdisc_dev(root) || !recur)
1877 		goto out;
1878 
1879 	hash_for_each(qdisc_dev(root)->qdisc_hash, b, q, hash) {
1880 		if (q_idx < s_q_idx) {
1881 			q_idx++;
1882 			continue;
1883 		}
1884 		if (!tc_qdisc_dump_ignore(q, dump_invisible, tcm))
1885 			ret = tc_fill_qdisc(skb, q, q->parent,
1886 					    NETLINK_CB(cb->skb).portid,
1887 					    nlh->nlmsg_seq, NLM_F_MULTI,
1888 					    RTM_NEWQDISC, NULL);
1889 		if (ret < 0)
1890 			goto out;
1891 		q_idx++;
1892 	}
1893 
1894 out:
1895 	*q_idx_p = q_idx;
1896 	return ret;
1897 }
1898 
1899 static int tc_dump_qdisc(struct sk_buff *skb, struct netlink_callback *cb)
1900 {
1901 	const struct nlmsghdr *nlh = cb->nlh;
1902 	struct net *net = sock_net(skb->sk);
1903 	struct nlattr *tca[TCA_MAX + 1];
1904 	struct {
1905 		unsigned long ifindex;
1906 		int q_idx;
1907 	} *ctx = (void *)cb->ctx;
1908 	const struct tcmsg *tcm;
1909 	struct net_device *dev;
1910 	int s_q_idx, q_idx;
1911 	int err;
1912 
1913 	ASSERT_RTNL();
1914 
1915 	err = nlmsg_parse_deprecated(nlh, sizeof(struct tcmsg), tca, TCA_MAX,
1916 				     rtm_tca_policy, cb->extack);
1917 	if (err < 0)
1918 		return err;
1919 	tcm = nlmsg_data(nlh);
1920 	if (tcm->tcm_ifindex && !ctx->ifindex)
1921 		ctx->ifindex = tcm->tcm_ifindex;
1922 
1923 	s_q_idx = ctx->q_idx;
1924 
1925 	for_each_netdev_dump(net, dev, ctx->ifindex) {
1926 		struct netdev_queue *dev_queue;
1927 		struct Qdisc *q;
1928 
1929 		if (tcm->tcm_ifindex && ctx->ifindex != tcm->tcm_ifindex)
1930 			break;
1931 
1932 		q_idx = 0;
1933 
1934 		netdev_lock_ops(dev);
1935 		q = rtnl_dereference(dev->qdisc);
1936 		err = tc_dump_qdisc_root(q, skb, cb, &q_idx, s_q_idx,
1937 					 true, tca[TCA_DUMP_INVISIBLE]);
1938 		if (err < 0)
1939 			goto error_unlock;
1940 
1941 		dev_queue = dev_ingress_queue(dev);
1942 		if (dev_queue) {
1943 			q = rtnl_dereference(dev_queue->qdisc_sleeping);
1944 			err = tc_dump_qdisc_root(q, skb, cb, &q_idx, s_q_idx,
1945 						 false, tca[TCA_DUMP_INVISIBLE]);
1946 			if (err < 0)
1947 				goto error_unlock;
1948 		}
1949 		netdev_unlock_ops(dev);
1950 		s_q_idx = 0;
1951 	}
1952 	return skb->len;
1953 
1954 error_unlock:
1955 	netdev_unlock_ops(dev);
1956 	ctx->q_idx = q_idx;
1957 
1958 	return err;
1959 }
1960 
1961 
1962 
1963 /************************************************
1964  *	Traffic classes manipulation.		*
1965  ************************************************/
1966 
1967 static int tc_fill_tclass(struct sk_buff *skb, struct Qdisc *q,
1968 			  unsigned long cl, u32 portid, u32 seq, u16 flags,
1969 			  int event, struct netlink_ext_ack *extack)
1970 {
1971 	struct tcmsg *tcm;
1972 	struct nlmsghdr  *nlh;
1973 	unsigned char *b = skb_tail_pointer(skb);
1974 	struct gnet_dump d;
1975 	const struct Qdisc_class_ops *cl_ops = q->ops->cl_ops;
1976 
1977 	cond_resched();
1978 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1979 	if (!nlh)
1980 		goto out_nlmsg_trim;
1981 	tcm = nlmsg_data(nlh);
1982 	tcm->tcm_family = AF_UNSPEC;
1983 	tcm->tcm__pad1 = 0;
1984 	tcm->tcm__pad2 = 0;
1985 	tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1986 	tcm->tcm_parent = q->handle;
1987 	tcm->tcm_handle = q->handle;
1988 	tcm->tcm_info = 0;
1989 	if (nla_put_string(skb, TCA_KIND, q->ops->id))
1990 		goto nla_put_failure;
1991 	if (cl_ops->dump && cl_ops->dump(q, cl, skb, tcm) < 0)
1992 		goto nla_put_failure;
1993 
1994 	if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS, TCA_XSTATS,
1995 					 NULL, &d, TCA_PAD) < 0)
1996 		goto nla_put_failure;
1997 
1998 	if (cl_ops->dump_stats && cl_ops->dump_stats(q, cl, &d) < 0)
1999 		goto nla_put_failure;
2000 
2001 	if (gnet_stats_finish_copy(&d) < 0)
2002 		goto nla_put_failure;
2003 
2004 	if (extack && extack->_msg &&
2005 	    nla_put_string(skb, TCA_EXT_WARN_MSG, extack->_msg))
2006 		goto out_nlmsg_trim;
2007 
2008 	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
2009 
2010 	return skb->len;
2011 
2012 out_nlmsg_trim:
2013 nla_put_failure:
2014 	nlmsg_trim(skb, b);
2015 	return -EMSGSIZE;
2016 }
2017 
2018 static int tclass_notify(struct net *net, struct sk_buff *oskb,
2019 			 struct nlmsghdr *n, struct Qdisc *q,
2020 			 unsigned long cl, int event, struct netlink_ext_ack *extack)
2021 {
2022 	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2023 	struct sk_buff *skb;
2024 	int ret;
2025 
2026 	if (!rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC))
2027 		return 0;
2028 
2029 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2030 	if (!skb)
2031 		return -ENOBUFS;
2032 
2033 	ret = tc_fill_tclass(skb, q, cl, portid, n->nlmsg_seq, 0, event, extack);
2034 	if (ret < 0) {
2035 		kfree_skb(skb);
2036 		return ret;
2037 	}
2038 
2039 	return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
2040 			      n->nlmsg_flags & NLM_F_ECHO);
2041 }
2042 
2043 static int tclass_get_notify(struct net *net, struct sk_buff *oskb,
2044 			     struct nlmsghdr *n, struct Qdisc *q,
2045 			     unsigned long cl, struct netlink_ext_ack *extack)
2046 {
2047 	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2048 	struct sk_buff *skb;
2049 	int ret;
2050 
2051 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2052 	if (!skb)
2053 		return -ENOBUFS;
2054 
2055 	ret = tc_fill_tclass(skb, q, cl, portid, n->nlmsg_seq, 0,
2056 			     RTM_NEWTCLASS, extack);
2057 	if (ret < 0) {
2058 		kfree_skb(skb);
2059 		return ret;
2060 	}
2061 
2062 	return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
2063 			      n->nlmsg_flags & NLM_F_ECHO);
2064 }
2065 
2066 static int tclass_del_notify(struct net *net,
2067 			     const struct Qdisc_class_ops *cops,
2068 			     struct sk_buff *oskb, struct nlmsghdr *n,
2069 			     struct Qdisc *q, unsigned long cl,
2070 			     struct netlink_ext_ack *extack)
2071 {
2072 	u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2073 	struct sk_buff *skb = NULL;
2074 	int err = 0;
2075 
2076 	if (!cops->delete)
2077 		return -EOPNOTSUPP;
2078 
2079 	if (rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC)) {
2080 		skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2081 		if (!skb)
2082 			return -ENOBUFS;
2083 
2084 		err = tc_fill_tclass(skb, q, cl, portid, n->nlmsg_seq, 0,
2085 				     RTM_DELTCLASS, extack);
2086 		if (err < 0) {
2087 			kfree_skb(skb);
2088 			return err;
2089 		}
2090 	}
2091 
2092 	err = cops->delete(q, cl, extack);
2093 	if (err) {
2094 		kfree_skb(skb);
2095 		return err;
2096 	}
2097 
2098 	err = rtnetlink_maybe_send(skb, net, portid, RTNLGRP_TC,
2099 				   n->nlmsg_flags & NLM_F_ECHO);
2100 	return err;
2101 }
2102 
2103 #ifdef CONFIG_NET_CLS
2104 
2105 struct tcf_bind_args {
2106 	struct tcf_walker w;
2107 	unsigned long base;
2108 	unsigned long cl;
2109 	u32 classid;
2110 };
2111 
2112 static int tcf_node_bind(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
2113 {
2114 	struct tcf_bind_args *a = (void *)arg;
2115 
2116 	if (n && tp->ops->bind_class) {
2117 		struct Qdisc *q = tcf_block_q(tp->chain->block);
2118 
2119 		sch_tree_lock(q);
2120 		tp->ops->bind_class(n, a->classid, a->cl, q, a->base);
2121 		sch_tree_unlock(q);
2122 	}
2123 	return 0;
2124 }
2125 
2126 struct tc_bind_class_args {
2127 	struct qdisc_walker w;
2128 	unsigned long new_cl;
2129 	u32 portid;
2130 	u32 clid;
2131 };
2132 
2133 static int tc_bind_class_walker(struct Qdisc *q, unsigned long cl,
2134 				struct qdisc_walker *w)
2135 {
2136 	struct tc_bind_class_args *a = (struct tc_bind_class_args *)w;
2137 	const struct Qdisc_class_ops *cops = q->ops->cl_ops;
2138 	struct tcf_block *block;
2139 	struct tcf_chain *chain;
2140 
2141 	block = cops->tcf_block(q, cl, NULL);
2142 	if (!block)
2143 		return 0;
2144 	for (chain = tcf_get_next_chain(block, NULL);
2145 	     chain;
2146 	     chain = tcf_get_next_chain(block, chain)) {
2147 		struct tcf_proto *tp;
2148 
2149 		for (tp = tcf_get_next_proto(chain, NULL);
2150 		     tp; tp = tcf_get_next_proto(chain, tp)) {
2151 			struct tcf_bind_args arg = {};
2152 
2153 			arg.w.fn = tcf_node_bind;
2154 			arg.classid = a->clid;
2155 			arg.base = cl;
2156 			arg.cl = a->new_cl;
2157 			tp->ops->walk(tp, &arg.w, true);
2158 		}
2159 	}
2160 
2161 	return 0;
2162 }
2163 
2164 static void tc_bind_tclass(struct Qdisc *q, u32 portid, u32 clid,
2165 			   unsigned long new_cl)
2166 {
2167 	const struct Qdisc_class_ops *cops = q->ops->cl_ops;
2168 	struct tc_bind_class_args args = {};
2169 
2170 	if (!cops->tcf_block)
2171 		return;
2172 	args.portid = portid;
2173 	args.clid = clid;
2174 	args.new_cl = new_cl;
2175 	args.w.fn = tc_bind_class_walker;
2176 	q->ops->cl_ops->walk(q, &args.w);
2177 }
2178 
2179 #else
2180 
2181 static void tc_bind_tclass(struct Qdisc *q, u32 portid, u32 clid,
2182 			   unsigned long new_cl)
2183 {
2184 }
2185 
2186 #endif
2187 
2188 static int __tc_ctl_tclass(struct sk_buff *skb, struct nlmsghdr *n,
2189 			   struct netlink_ext_ack *extack,
2190 			   struct net_device *dev,
2191 			   struct nlattr *tca[TCA_MAX + 1],
2192 			   struct tcmsg *tcm)
2193 {
2194 	struct net *net = sock_net(skb->sk);
2195 	const struct Qdisc_class_ops *cops;
2196 	struct Qdisc *q = NULL;
2197 	unsigned long cl = 0;
2198 	unsigned long new_cl;
2199 	u32 portid;
2200 	u32 clid;
2201 	u32 qid;
2202 	int err;
2203 
2204 	/*
2205 	   parent == TC_H_UNSPEC - unspecified parent.
2206 	   parent == TC_H_ROOT   - class is root, which has no parent.
2207 	   parent == X:0	 - parent is root class.
2208 	   parent == X:Y	 - parent is a node in hierarchy.
2209 	   parent == 0:Y	 - parent is X:Y, where X:0 is qdisc.
2210 
2211 	   handle == 0:0	 - generate handle from kernel pool.
2212 	   handle == 0:Y	 - class is X:Y, where X:0 is qdisc.
2213 	   handle == X:Y	 - clear.
2214 	   handle == X:0	 - root class.
2215 	 */
2216 
2217 	/* Step 1. Determine qdisc handle X:0 */
2218 
2219 	portid = tcm->tcm_parent;
2220 	clid = tcm->tcm_handle;
2221 	qid = TC_H_MAJ(clid);
2222 
2223 	if (portid != TC_H_ROOT) {
2224 		u32 qid1 = TC_H_MAJ(portid);
2225 
2226 		if (qid && qid1) {
2227 			/* If both majors are known, they must be identical. */
2228 			if (qid != qid1)
2229 				return -EINVAL;
2230 		} else if (qid1) {
2231 			qid = qid1;
2232 		} else if (qid == 0)
2233 			qid = rtnl_dereference(dev->qdisc)->handle;
2234 
2235 		/* Now qid is genuine qdisc handle consistent
2236 		 * both with parent and child.
2237 		 *
2238 		 * TC_H_MAJ(portid) still may be unspecified, complete it now.
2239 		 */
2240 		if (portid)
2241 			portid = TC_H_MAKE(qid, portid);
2242 	} else {
2243 		if (qid == 0)
2244 			qid = rtnl_dereference(dev->qdisc)->handle;
2245 	}
2246 
2247 	/* OK. Locate qdisc */
2248 	q = qdisc_lookup(dev, qid);
2249 	if (!q)
2250 		return -ENOENT;
2251 
2252 	/* An check that it supports classes */
2253 	cops = q->ops->cl_ops;
2254 	if (cops == NULL)
2255 		return -EINVAL;
2256 
2257 	/* Now try to get class */
2258 	if (clid == 0) {
2259 		if (portid == TC_H_ROOT)
2260 			clid = qid;
2261 	} else
2262 		clid = TC_H_MAKE(qid, clid);
2263 
2264 	if (clid)
2265 		cl = cops->find(q, clid);
2266 
2267 	if (cl == 0) {
2268 		err = -ENOENT;
2269 		if (n->nlmsg_type != RTM_NEWTCLASS ||
2270 		    !(n->nlmsg_flags & NLM_F_CREATE))
2271 			goto out;
2272 	} else {
2273 		switch (n->nlmsg_type) {
2274 		case RTM_NEWTCLASS:
2275 			err = -EEXIST;
2276 			if (n->nlmsg_flags & NLM_F_EXCL)
2277 				goto out;
2278 			break;
2279 		case RTM_DELTCLASS:
2280 			err = tclass_del_notify(net, cops, skb, n, q, cl, extack);
2281 			/* Unbind the class with flilters with 0 */
2282 			tc_bind_tclass(q, portid, clid, 0);
2283 			goto out;
2284 		case RTM_GETTCLASS:
2285 			err = tclass_get_notify(net, skb, n, q, cl, extack);
2286 			goto out;
2287 		default:
2288 			err = -EINVAL;
2289 			goto out;
2290 		}
2291 	}
2292 
2293 	if (tca[TCA_INGRESS_BLOCK] || tca[TCA_EGRESS_BLOCK]) {
2294 		NL_SET_ERR_MSG(extack, "Shared blocks are not supported for classes");
2295 		return -EOPNOTSUPP;
2296 	}
2297 
2298 	/* Prevent creation of traffic classes with classid TC_H_ROOT */
2299 	if (clid == TC_H_ROOT) {
2300 		NL_SET_ERR_MSG(extack, "Cannot create traffic class with classid TC_H_ROOT");
2301 		return -EINVAL;
2302 	}
2303 
2304 	new_cl = cl;
2305 	err = -EOPNOTSUPP;
2306 	if (cops->change)
2307 		err = cops->change(q, clid, portid, tca, &new_cl, extack);
2308 	if (err == 0) {
2309 		tclass_notify(net, skb, n, q, new_cl, RTM_NEWTCLASS, extack);
2310 		/* We just create a new class, need to do reverse binding. */
2311 		if (cl != new_cl)
2312 			tc_bind_tclass(q, portid, clid, new_cl);
2313 	}
2314 out:
2315 	return err;
2316 }
2317 
2318 static int tc_ctl_tclass(struct sk_buff *skb, struct nlmsghdr *n,
2319 			 struct netlink_ext_ack *extack)
2320 {
2321 	struct net *net = sock_net(skb->sk);
2322 	struct tcmsg *tcm = nlmsg_data(n);
2323 	struct nlattr *tca[TCA_MAX + 1];
2324 	struct net_device *dev;
2325 	int err;
2326 
2327 	err = nlmsg_parse_deprecated(n, sizeof(*tcm), tca, TCA_MAX,
2328 				     rtm_tca_policy, extack);
2329 	if (err < 0)
2330 		return err;
2331 
2332 	dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2333 	if (!dev)
2334 		return -ENODEV;
2335 
2336 	netdev_lock_ops(dev);
2337 	err = __tc_ctl_tclass(skb, n, extack, dev, tca, tcm);
2338 	netdev_unlock_ops(dev);
2339 
2340 	return err;
2341 }
2342 
2343 struct qdisc_dump_args {
2344 	struct qdisc_walker	w;
2345 	struct sk_buff		*skb;
2346 	struct netlink_callback	*cb;
2347 };
2348 
2349 static int qdisc_class_dump(struct Qdisc *q, unsigned long cl,
2350 			    struct qdisc_walker *arg)
2351 {
2352 	struct qdisc_dump_args *a = (struct qdisc_dump_args *)arg;
2353 
2354 	return tc_fill_tclass(a->skb, q, cl, NETLINK_CB(a->cb->skb).portid,
2355 			      a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
2356 			      RTM_NEWTCLASS, NULL);
2357 }
2358 
2359 static int tc_dump_tclass_qdisc(struct Qdisc *q, struct sk_buff *skb,
2360 				struct tcmsg *tcm, struct netlink_callback *cb,
2361 				int *t_p, int s_t)
2362 {
2363 	struct qdisc_dump_args arg;
2364 
2365 	if (tc_qdisc_dump_ignore(q, false, NULL) ||
2366 	    *t_p < s_t || !q->ops->cl_ops ||
2367 	    (tcm->tcm_parent &&
2368 	     TC_H_MAJ(tcm->tcm_parent) != q->handle)) {
2369 		(*t_p)++;
2370 		return 0;
2371 	}
2372 	if (*t_p > s_t)
2373 		memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
2374 	arg.w.fn = qdisc_class_dump;
2375 	arg.skb = skb;
2376 	arg.cb = cb;
2377 	arg.w.stop  = 0;
2378 	arg.w.skip = cb->args[1];
2379 	arg.w.count = 0;
2380 	q->ops->cl_ops->walk(q, &arg.w);
2381 	cb->args[1] = arg.w.count;
2382 	if (arg.w.stop)
2383 		return -1;
2384 	(*t_p)++;
2385 	return 0;
2386 }
2387 
2388 static int tc_dump_tclass_root(struct Qdisc *root, struct sk_buff *skb,
2389 			       struct tcmsg *tcm, struct netlink_callback *cb,
2390 			       int *t_p, int s_t, bool recur)
2391 {
2392 	struct Qdisc *q;
2393 	int b;
2394 
2395 	if (!root)
2396 		return 0;
2397 
2398 	if (tc_dump_tclass_qdisc(root, skb, tcm, cb, t_p, s_t) < 0)
2399 		return -1;
2400 
2401 	if (!qdisc_dev(root) || !recur)
2402 		return 0;
2403 
2404 	if (tcm->tcm_parent) {
2405 		q = qdisc_match_from_root(root, TC_H_MAJ(tcm->tcm_parent));
2406 		if (q && q != root &&
2407 		    tc_dump_tclass_qdisc(q, skb, tcm, cb, t_p, s_t) < 0)
2408 			return -1;
2409 		return 0;
2410 	}
2411 	hash_for_each(qdisc_dev(root)->qdisc_hash, b, q, hash) {
2412 		if (tc_dump_tclass_qdisc(q, skb, tcm, cb, t_p, s_t) < 0)
2413 			return -1;
2414 	}
2415 
2416 	return 0;
2417 }
2418 
2419 static int __tc_dump_tclass(struct sk_buff *skb, struct netlink_callback *cb,
2420 			    struct tcmsg *tcm, struct net_device *dev)
2421 {
2422 	struct netdev_queue *dev_queue;
2423 	int t, s_t;
2424 
2425 	s_t = cb->args[0];
2426 	t = 0;
2427 
2428 	if (tc_dump_tclass_root(rtnl_dereference(dev->qdisc),
2429 				skb, tcm, cb, &t, s_t, true) < 0)
2430 		goto done;
2431 
2432 	dev_queue = dev_ingress_queue(dev);
2433 	if (dev_queue &&
2434 	    tc_dump_tclass_root(rtnl_dereference(dev_queue->qdisc_sleeping),
2435 				skb, tcm, cb, &t, s_t, false) < 0)
2436 		goto done;
2437 
2438 done:
2439 	cb->args[0] = t;
2440 
2441 	return skb->len;
2442 }
2443 
2444 static int tc_dump_tclass(struct sk_buff *skb, struct netlink_callback *cb)
2445 {
2446 	struct tcmsg *tcm = nlmsg_data(cb->nlh);
2447 	struct net *net = sock_net(skb->sk);
2448 	struct net_device *dev;
2449 	int err;
2450 
2451 	if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2452 		return 0;
2453 
2454 	dev = dev_get_by_index(net, tcm->tcm_ifindex);
2455 	if (!dev)
2456 		return 0;
2457 
2458 	netdev_lock_ops(dev);
2459 	err = __tc_dump_tclass(skb, cb, tcm, dev);
2460 	netdev_unlock_ops(dev);
2461 
2462 	dev_put(dev);
2463 
2464 	return err;
2465 }
2466 
2467 #ifdef CONFIG_PROC_FS
2468 static int psched_show(struct seq_file *seq, void *v)
2469 {
2470 	seq_printf(seq, "%08x %08x %08x %08x\n",
2471 		   (u32)NSEC_PER_USEC, (u32)PSCHED_TICKS2NS(1),
2472 		   1000000,
2473 		   (u32)NSEC_PER_SEC / hrtimer_resolution);
2474 
2475 	return 0;
2476 }
2477 
2478 static int __net_init psched_net_init(struct net *net)
2479 {
2480 	struct proc_dir_entry *e;
2481 
2482 	e = proc_create_single("psched", 0, net->proc_net, psched_show);
2483 	if (e == NULL)
2484 		return -ENOMEM;
2485 
2486 	return 0;
2487 }
2488 
2489 static void __net_exit psched_net_exit(struct net *net)
2490 {
2491 	remove_proc_entry("psched", net->proc_net);
2492 }
2493 #else
2494 static int __net_init psched_net_init(struct net *net)
2495 {
2496 	return 0;
2497 }
2498 
2499 static void __net_exit psched_net_exit(struct net *net)
2500 {
2501 }
2502 #endif
2503 
2504 static struct pernet_operations psched_net_ops = {
2505 	.init = psched_net_init,
2506 	.exit = psched_net_exit,
2507 };
2508 
2509 #if IS_ENABLED(CONFIG_MITIGATION_RETPOLINE)
2510 DEFINE_STATIC_KEY_FALSE(tc_skip_wrapper_act);
2511 DEFINE_STATIC_KEY_FALSE(tc_skip_wrapper_cls);
2512 #endif
2513 
2514 static const struct rtnl_msg_handler psched_rtnl_msg_handlers[] __initconst = {
2515 	{.msgtype = RTM_NEWQDISC, .doit = tc_modify_qdisc},
2516 	{.msgtype = RTM_DELQDISC, .doit = tc_get_qdisc},
2517 	{.msgtype = RTM_GETQDISC, .doit = tc_get_qdisc,
2518 	 .dumpit = tc_dump_qdisc},
2519 	{.msgtype = RTM_NEWTCLASS, .doit = tc_ctl_tclass},
2520 	{.msgtype = RTM_DELTCLASS, .doit = tc_ctl_tclass},
2521 	{.msgtype = RTM_GETTCLASS, .doit = tc_ctl_tclass,
2522 	 .dumpit = tc_dump_tclass},
2523 };
2524 
2525 static int __init pktsched_init(void)
2526 {
2527 	int err;
2528 
2529 	err = register_pernet_subsys(&psched_net_ops);
2530 	if (err) {
2531 		pr_err("pktsched_init: "
2532 		       "cannot initialize per netns operations\n");
2533 		return err;
2534 	}
2535 
2536 	register_qdisc(&pfifo_fast_ops);
2537 	register_qdisc(&pfifo_qdisc_ops);
2538 	register_qdisc(&bfifo_qdisc_ops);
2539 	register_qdisc(&pfifo_head_drop_qdisc_ops);
2540 	register_qdisc(&mq_qdisc_ops);
2541 	register_qdisc(&noqueue_qdisc_ops);
2542 
2543 	rtnl_register_many(psched_rtnl_msg_handlers);
2544 
2545 	tc_wrapper_init();
2546 
2547 	return 0;
2548 }
2549 
2550 subsys_initcall(pktsched_init);
2551