Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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