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_htb.c	Hierarchical token bucket, feed tree version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Authors:	Martin Devera, <devik@cdi.cz>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * Credits (in time order) for older HTB versions:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  *              Stef Coene <stef.coene@docum.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *			HTB support at LARTC mailing list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *		Ondrej Kraus, <krauso@barr.cz>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *			found missing INIT_QDISC(htb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  *		Vladimir Smelhaus, Aamer Akhter, Bert Hubert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  *			helped a lot to locate nasty class stall bug
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  *		Andi Kleen, Jamal Hadi, Bert Hubert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  *			code review and helpful comments on shaping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  *		Tomasz Wrona, <tw@eter.tym.pl>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  *			created test case so that I was able to fix nasty bug
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  *		Wilfried Weissmann
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  *			spotted bug in dequeue code and helped with fix
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  *		Jiri Fojtasek
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  *			fixed requeue routine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  *		and many others. thanks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/rbtree.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <net/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include <net/sch_generic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include <net/pkt_sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #include <net/pkt_cls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) /* HTB algorithm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)     Author: devik@cdi.cz
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)     ========================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)     HTB is like TBF with multiple classes. It is also similar to CBQ because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45)     it allows to assign priority to each class in hierarchy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46)     In fact it is another implementation of Floyd's formal sharing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48)     Levels:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49)     Each class is assigned level. Leaf has ALWAYS level 0 and root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50)     classes have level TC_HTB_MAXDEPTH-1. Interior nodes has level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)     one less than their parent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) static int htb_hysteresis __read_mostly = 0; /* whether to use mode hysteresis for speedup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) #define HTB_VER 0x30011		/* major must be matched with number suplied by TC as version */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) #if HTB_VER >> 16 != TC_HTB_PROTOVER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) #error "Mismatched sch_htb.c and pkt_sch.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) /* Module parameter and sysfs export */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) module_param    (htb_hysteresis, int, 0640);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) MODULE_PARM_DESC(htb_hysteresis, "Hysteresis mode, less CPU load, less accurate");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) static int htb_rate_est = 0; /* htb classes have a default rate estimator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) module_param(htb_rate_est, int, 0640);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) MODULE_PARM_DESC(htb_rate_est, "setup a default rate estimator (4sec 16sec) for htb classes");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) /* used internaly to keep status of single class */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) enum htb_cmode {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	HTB_CANT_SEND,		/* class can't send and can't borrow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	HTB_MAY_BORROW,		/* class can't send but may borrow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	HTB_CAN_SEND		/* class can send */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) struct htb_prio {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 		struct rb_root	row;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 		struct rb_root	feed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	struct rb_node	*ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	/* When class changes from state 1->2 and disconnects from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	 * parent's feed then we lost ptr value and start from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	 * first child again. Here we store classid of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	 * last valid ptr (used when ptr is NULL).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	u32		last_ptr_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) /* interior & leaf nodes; props specific to leaves are marked L:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91)  * To reduce false sharing, place mostly read fields at beginning,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92)  * and mostly written ones at the end.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) struct htb_class {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	struct Qdisc_class_common common;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	struct psched_ratecfg	rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	struct psched_ratecfg	ceil;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	s64			buffer, cbuffer;/* token bucket depth/rate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	s64			mbuffer;	/* max wait time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	u32			prio;		/* these two are used only by leaves... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	int			quantum;	/* but stored for parent-to-leaf return */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	struct tcf_proto __rcu	*filter_list;	/* class attached filters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	struct tcf_block	*block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	int			filter_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	int			level;		/* our level (see above) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	unsigned int		children;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	struct htb_class	*parent;	/* parent class */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	struct net_rate_estimator __rcu *rate_est;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	 * Written often fields
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	struct gnet_stats_basic_packed bstats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	struct tc_htb_xstats	xstats;	/* our special stats */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	/* token bucket parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	s64			tokens, ctokens;/* current number of tokens */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	s64			t_c;		/* checkpoint time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 		struct htb_class_leaf {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 			int		deficit[TC_HTB_MAXDEPTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 			struct Qdisc	*q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 		} leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 		struct htb_class_inner {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 			struct htb_prio clprio[TC_HTB_NUMPRIO];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 		} inner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	s64			pq_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	int			prio_activity;	/* for which prios are we active */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	enum htb_cmode		cmode;		/* current mode of the class */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	struct rb_node		pq_node;	/* node for event queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	struct rb_node		node[TC_HTB_NUMPRIO];	/* node for self or feed tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	unsigned int drops ____cacheline_aligned_in_smp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	unsigned int		overlimits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) struct htb_level {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	struct rb_root	wait_pq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	struct htb_prio hprio[TC_HTB_NUMPRIO];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) struct htb_sched {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	struct Qdisc_class_hash clhash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	int			defcls;		/* class where unclassified flows go to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	int			rate2quantum;	/* quant = rate / rate2quantum */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	/* filters for qdisc itself */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	struct tcf_proto __rcu	*filter_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	struct tcf_block	*block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) #define HTB_WARN_TOOMANYEVENTS	0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	unsigned int		warned;	/* only one warning */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	int			direct_qlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	struct work_struct	work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	/* non shaped skbs; let them go directly thru */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	struct qdisc_skb_head	direct_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	u32			direct_pkts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	u32			overlimits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	struct qdisc_watchdog	watchdog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	s64			now;	/* cached dequeue time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	/* time of nearest event per level (row) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	s64			near_ev_cache[TC_HTB_MAXDEPTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	int			row_mask[TC_HTB_MAXDEPTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	struct htb_level	hlevel[TC_HTB_MAXDEPTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) /* find class in global hash table using given handle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) static inline struct htb_class *htb_find(u32 handle, struct Qdisc *sch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	struct htb_sched *q = qdisc_priv(sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	struct Qdisc_class_common *clc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	clc = qdisc_class_find(&q->clhash, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	if (clc == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	return container_of(clc, struct htb_class, common);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) static unsigned long htb_search(struct Qdisc *sch, u32 handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	return (unsigned long)htb_find(handle, sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196)  * htb_classify - classify a packet into class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198)  * It returns NULL if the packet should be dropped or -1 if the packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199)  * should be passed directly thru. In all other cases leaf class is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200)  * We allow direct class selection by classid in priority. The we examine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201)  * filters in qdisc and in inner nodes (if higher filter points to the inner
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202)  * node). If we end up with classid MAJOR:0 we enqueue the skb into special
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203)  * internal fifo (direct). These packets then go directly thru. If we still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204)  * have no valid leaf we try to use MAJOR:default leaf. It still unsuccessful
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205)  * then finish and return direct queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) #define HTB_DIRECT ((struct htb_class *)-1L)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) static struct htb_class *htb_classify(struct sk_buff *skb, struct Qdisc *sch,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 				      int *qerr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	struct htb_sched *q = qdisc_priv(sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	struct htb_class *cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	struct tcf_result res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	struct tcf_proto *tcf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	/* allow to select class by setting skb->priority to valid classid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	 * note that nfmark can be used too by attaching filter fw with no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	 * rules in it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	if (skb->priority == sch->handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 		return HTB_DIRECT;	/* X:0 (direct flow) selected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	cl = htb_find(skb->priority, sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	if (cl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		if (cl->level == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 			return cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 		/* Start with inner filter chain if a non-leaf class is selected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 		tcf = rcu_dereference_bh(cl->filter_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 		tcf = rcu_dereference_bh(q->filter_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	*qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	while (tcf && (result = tcf_classify(skb, tcf, &res, false)) >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) #ifdef CONFIG_NET_CLS_ACT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		switch (result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		case TC_ACT_QUEUED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 		case TC_ACT_STOLEN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 		case TC_ACT_TRAP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 			*qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 			fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		case TC_ACT_SHOT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 		cl = (void *)res.class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 		if (!cl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 			if (res.classid == sch->handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 				return HTB_DIRECT;	/* X:0 (direct flow) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 			cl = htb_find(res.classid, sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 			if (!cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 				break;	/* filter selected invalid classid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 		if (!cl->level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 			return cl;	/* we hit leaf; return it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 		/* we have got inner class; apply inner filter chain */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 		tcf = rcu_dereference_bh(cl->filter_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	/* classification failed; try to use default class */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	cl = htb_find(TC_H_MAKE(TC_H_MAJ(sch->handle), q->defcls), sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	if (!cl || cl->level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		return HTB_DIRECT;	/* bad default .. this is safe bet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	return cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269)  * htb_add_to_id_tree - adds class to the round robin list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271)  * Routine adds class to the list (actually tree) sorted by classid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272)  * Make sure that class is not already on such list for given prio.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) static void htb_add_to_id_tree(struct rb_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 			       struct htb_class *cl, int prio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	struct rb_node **p = &root->rb_node, *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 		struct htb_class *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		parent = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 		c = rb_entry(parent, struct htb_class, node[prio]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 		if (cl->common.classid > c->common.classid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 			p = &parent->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 			p = &parent->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	rb_link_node(&cl->node[prio], parent, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	rb_insert_color(&cl->node[prio], root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294)  * htb_add_to_wait_tree - adds class to the event queue with delay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296)  * The class is added to priority event queue to indicate that class will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297)  * change its mode in cl->pq_key microseconds. Make sure that class is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298)  * already in the queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) static void htb_add_to_wait_tree(struct htb_sched *q,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 				 struct htb_class *cl, s64 delay)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	struct rb_node **p = &q->hlevel[cl->level].wait_pq.rb_node, *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	cl->pq_key = q->now + delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	if (cl->pq_key == q->now)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 		cl->pq_key++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	/* update the nearest event cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	if (q->near_ev_cache[cl->level] > cl->pq_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 		q->near_ev_cache[cl->level] = cl->pq_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 		struct htb_class *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 		parent = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 		c = rb_entry(parent, struct htb_class, pq_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 		if (cl->pq_key >= c->pq_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 			p = &parent->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 			p = &parent->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	rb_link_node(&cl->pq_node, parent, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	rb_insert_color(&cl->pq_node, &q->hlevel[cl->level].wait_pq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327)  * htb_next_rb_node - finds next node in binary tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329)  * When we are past last key we return NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330)  * Average complexity is 2 steps per call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) static inline void htb_next_rb_node(struct rb_node **n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	*n = rb_next(*n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338)  * htb_add_class_to_row - add class to its row
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340)  * The class is added to row at priorities marked in mask.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341)  * It does nothing if mask == 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) static inline void htb_add_class_to_row(struct htb_sched *q,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 					struct htb_class *cl, int mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	q->row_mask[cl->level] |= mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	while (mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		int prio = ffz(~mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 		mask &= ~(1 << prio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		htb_add_to_id_tree(&q->hlevel[cl->level].hprio[prio].row, cl, prio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) /* If this triggers, it is a bug in this code, but it need not be fatal */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) static void htb_safe_rb_erase(struct rb_node *rb, struct rb_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	if (RB_EMPTY_NODE(rb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		rb_erase(rb, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 		RB_CLEAR_NODE(rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367)  * htb_remove_class_from_row - removes class from its row
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369)  * The class is removed from row at priorities marked in mask.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370)  * It does nothing if mask == 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) static inline void htb_remove_class_from_row(struct htb_sched *q,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 						 struct htb_class *cl, int mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	int m = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	struct htb_level *hlevel = &q->hlevel[cl->level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	while (mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 		int prio = ffz(~mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		struct htb_prio *hprio = &hlevel->hprio[prio];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 		mask &= ~(1 << prio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 		if (hprio->ptr == cl->node + prio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 			htb_next_rb_node(&hprio->ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 		htb_safe_rb_erase(cl->node + prio, &hprio->row);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 		if (!hprio->row.rb_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 			m |= 1 << prio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	q->row_mask[cl->level] &= ~m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394)  * htb_activate_prios - creates active classe's feed chain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396)  * The class is connected to ancestors and/or appropriate rows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397)  * for priorities it is participating on. cl->cmode must be new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398)  * (activated) mode. It does nothing if cl->prio_activity == 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) static void htb_activate_prios(struct htb_sched *q, struct htb_class *cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	struct htb_class *p = cl->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	long m, mask = cl->prio_activity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	while (cl->cmode == HTB_MAY_BORROW && p && mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 		m = mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 		while (m) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 			int prio = ffz(~m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 			m &= ~(1 << prio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 			if (p->inner.clprio[prio].feed.rb_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 				/* parent already has its feed in use so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 				 * reset bit in mask as parent is already ok
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 				mask &= ~(1 << prio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 			htb_add_to_id_tree(&p->inner.clprio[prio].feed, cl, prio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 		p->prio_activity |= mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 		cl = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		p = cl->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	if (cl->cmode == HTB_CAN_SEND && mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		htb_add_class_to_row(q, cl, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) }
^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)  * htb_deactivate_prios - remove class from feed chain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431)  * cl->cmode must represent old mode (before deactivation). It does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432)  * nothing if cl->prio_activity == 0. Class is removed from all feed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433)  * chains and rows.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) static void htb_deactivate_prios(struct htb_sched *q, struct htb_class *cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	struct htb_class *p = cl->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	long m, mask = cl->prio_activity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	while (cl->cmode == HTB_MAY_BORROW && p && mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 		m = mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 		while (m) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 			int prio = ffz(~m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 			m &= ~(1 << prio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 			if (p->inner.clprio[prio].ptr == cl->node + prio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 				/* we are removing child which is pointed to from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 				 * parent feed - forget the pointer but remember
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 				 * classid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 				p->inner.clprio[prio].last_ptr_id = cl->common.classid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 				p->inner.clprio[prio].ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 			htb_safe_rb_erase(cl->node + prio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 					  &p->inner.clprio[prio].feed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 			if (!p->inner.clprio[prio].feed.rb_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 				mask |= 1 << prio;
^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) 		p->prio_activity &= ~mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 		cl = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 		p = cl->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	if (cl->cmode == HTB_CAN_SEND && mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 		htb_remove_class_from_row(q, cl, mask);
^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 inline s64 htb_lowater(const struct htb_class *cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	if (htb_hysteresis)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 		return cl->cmode != HTB_CANT_SEND ? -cl->cbuffer : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) static inline s64 htb_hiwater(const struct htb_class *cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	if (htb_hysteresis)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		return cl->cmode == HTB_CAN_SEND ? -cl->buffer : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489)  * htb_class_mode - computes and returns current class mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491)  * It computes cl's mode at time cl->t_c+diff and returns it. If mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492)  * is not HTB_CAN_SEND then cl->pq_key is updated to time difference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493)  * from now to time when cl will change its state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494)  * Also it is worth to note that class mode doesn't change simply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495)  * at cl->{c,}tokens == 0 but there can rather be hysteresis of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496)  * 0 .. -cl->{c,}buffer range. It is meant to limit number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497)  * mode transitions per time unit. The speed gain is about 1/6.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) static inline enum htb_cmode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) htb_class_mode(struct htb_class *cl, s64 *diff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	s64 toks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	if ((toks = (cl->ctokens + *diff)) < htb_lowater(cl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 		*diff = -toks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 		return HTB_CANT_SEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	if ((toks = (cl->tokens + *diff)) >= htb_hiwater(cl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		return HTB_CAN_SEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	*diff = -toks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	return HTB_MAY_BORROW;
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517)  * htb_change_class_mode - changes classe's mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519)  * This should be the only way how to change classe's mode under normal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520)  * cirsumstances. Routine will update feed lists linkage, change mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521)  * and add class to the wait event queue if appropriate. New mode should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522)  * be different from old one and cl->pq_key has to be valid if changing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523)  * to mode other than HTB_CAN_SEND (see htb_add_to_wait_tree).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) htb_change_class_mode(struct htb_sched *q, struct htb_class *cl, s64 *diff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	enum htb_cmode new_mode = htb_class_mode(cl, diff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	if (new_mode == cl->cmode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	if (new_mode == HTB_CANT_SEND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		cl->overlimits++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 		q->overlimits++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	if (cl->prio_activity) {	/* not necessary: speed optimization */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		if (cl->cmode != HTB_CANT_SEND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 			htb_deactivate_prios(q, cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 		cl->cmode = new_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		if (new_mode != HTB_CANT_SEND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 			htb_activate_prios(q, cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 		cl->cmode = new_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549)  * htb_activate - inserts leaf cl into appropriate active feeds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551)  * Routine learns (new) priority of leaf and activates feed chain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552)  * for the prio. It can be called on already active leaf safely.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553)  * It also adds leaf into droplist.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) static inline void htb_activate(struct htb_sched *q, struct htb_class *cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	WARN_ON(cl->level || !cl->leaf.q || !cl->leaf.q->q.qlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	if (!cl->prio_activity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 		cl->prio_activity = 1 << cl->prio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		htb_activate_prios(q, cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	}
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566)  * htb_deactivate - remove leaf cl from active feeds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568)  * Make sure that leaf is active. In the other words it can't be called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569)  * with non-active leaf. It also removes class from the drop list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) static inline void htb_deactivate(struct htb_sched *q, struct htb_class *cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	WARN_ON(!cl->prio_activity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	htb_deactivate_prios(q, cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	cl->prio_activity = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) static int htb_enqueue(struct sk_buff *skb, struct Qdisc *sch,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 		       struct sk_buff **to_free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	unsigned int len = qdisc_pkt_len(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	struct htb_sched *q = qdisc_priv(sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	struct htb_class *cl = htb_classify(skb, sch, &ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	if (cl == HTB_DIRECT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		/* enqueue to helper queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 		if (q->direct_queue.qlen < q->direct_qlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 			__qdisc_enqueue_tail(skb, &q->direct_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 			q->direct_pkts++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 			return qdisc_drop(skb, sch, to_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) #ifdef CONFIG_NET_CLS_ACT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	} else if (!cl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 		if (ret & __NET_XMIT_BYPASS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 			qdisc_qstats_drop(sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		__qdisc_drop(skb, to_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	} else if ((ret = qdisc_enqueue(skb, cl->leaf.q,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 					to_free)) != NET_XMIT_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 		if (net_xmit_drop_count(ret)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 			qdisc_qstats_drop(sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 			cl->drops++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 		htb_activate(q, cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	sch->qstats.backlog += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	sch->q.qlen++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	return NET_XMIT_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) static inline void htb_accnt_tokens(struct htb_class *cl, int bytes, s64 diff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	s64 toks = diff + cl->tokens;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	if (toks > cl->buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		toks = cl->buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	toks -= (s64) psched_l2t_ns(&cl->rate, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	if (toks <= -cl->mbuffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		toks = 1 - cl->mbuffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	cl->tokens = toks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) static inline void htb_accnt_ctokens(struct htb_class *cl, int bytes, s64 diff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	s64 toks = diff + cl->ctokens;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	if (toks > cl->cbuffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 		toks = cl->cbuffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	toks -= (s64) psched_l2t_ns(&cl->ceil, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	if (toks <= -cl->mbuffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		toks = 1 - cl->mbuffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	cl->ctokens = toks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645)  * htb_charge_class - charges amount "bytes" to leaf and ancestors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647)  * Routine assumes that packet "bytes" long was dequeued from leaf cl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648)  * borrowing from "level". It accounts bytes to ceil leaky bucket for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649)  * leaf and all ancestors and to rate bucket for ancestors at levels
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650)  * "level" and higher. It also handles possible change of mode resulting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651)  * from the update. Note that mode can also increase here (MAY_BORROW to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652)  * CAN_SEND) because we can use more precise clock that event queue here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653)  * In such case we remove class from event queue first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) static void htb_charge_class(struct htb_sched *q, struct htb_class *cl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 			     int level, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	int bytes = qdisc_pkt_len(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	enum htb_cmode old_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	s64 diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	while (cl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		diff = min_t(s64, q->now - cl->t_c, cl->mbuffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 		if (cl->level >= level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 			if (cl->level == level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 				cl->xstats.lends++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 			htb_accnt_tokens(cl, bytes, diff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 			cl->xstats.borrows++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 			cl->tokens += diff;	/* we moved t_c; update tokens */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 		htb_accnt_ctokens(cl, bytes, diff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		cl->t_c = q->now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		old_mode = cl->cmode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		diff = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 		htb_change_class_mode(q, cl, &diff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		if (old_mode != cl->cmode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 			if (old_mode != HTB_CAN_SEND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 				htb_safe_rb_erase(&cl->pq_node, &q->hlevel[cl->level].wait_pq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 			if (cl->cmode != HTB_CAN_SEND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 				htb_add_to_wait_tree(q, cl, diff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 		/* update basic stats except for leaves which are already updated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 		if (cl->level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 			bstats_update(&cl->bstats, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 		cl = cl->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694)  * htb_do_events - make mode changes to classes at the level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696)  * Scans event queue for pending events and applies them. Returns time of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697)  * next pending event (0 for no event in pq, q->now for too many events).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698)  * Note: Applied are events whose have cl->pq_key <= q->now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) static s64 htb_do_events(struct htb_sched *q, const int level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 			 unsigned long start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	/* don't run for longer than 2 jiffies; 2 is used instead of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	 * 1 to simplify things when jiffy is going to be incremented
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	 * too soon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	unsigned long stop_at = start + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	struct rb_root *wait_pq = &q->hlevel[level].wait_pq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	while (time_before(jiffies, stop_at)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 		struct htb_class *cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 		s64 diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 		struct rb_node *p = rb_first(wait_pq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 		if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 		cl = rb_entry(p, struct htb_class, pq_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		if (cl->pq_key > q->now)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 			return cl->pq_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		htb_safe_rb_erase(p, wait_pq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		diff = min_t(s64, q->now - cl->t_c, cl->mbuffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		htb_change_class_mode(q, cl, &diff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		if (cl->cmode != HTB_CAN_SEND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 			htb_add_to_wait_tree(q, cl, diff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	/* too much load - let's continue after a break for scheduling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	if (!(q->warned & HTB_WARN_TOOMANYEVENTS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		pr_warn("htb: too many events!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 		q->warned |= HTB_WARN_TOOMANYEVENTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	return q->now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) /* Returns class->node+prio from id-tree where classe's id is >= id. NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739)  * is no such one exists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) static struct rb_node *htb_id_find_next_upper(int prio, struct rb_node *n,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 					      u32 id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	struct rb_node *r = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	while (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 		struct htb_class *cl =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		    rb_entry(n, struct htb_class, node[prio]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		if (id > cl->common.classid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 			n = n->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		} else if (id < cl->common.classid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 			r = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 			n = n->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 			return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762)  * htb_lookup_leaf - returns next leaf class in DRR order
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764)  * Find leaf where current feed pointers points to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) static struct htb_class *htb_lookup_leaf(struct htb_prio *hprio, const int prio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 		struct rb_node *root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		struct rb_node **pptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		u32 *pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	} stk[TC_HTB_MAXDEPTH], *sp = stk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	BUG_ON(!hprio->row.rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	sp->root = hprio->row.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	sp->pptr = &hprio->ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	sp->pid = &hprio->last_ptr_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	for (i = 0; i < 65535; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 		if (!*sp->pptr && *sp->pid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 			/* ptr was invalidated but id is valid - try to recover
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 			 * the original or next ptr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 			*sp->pptr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 			    htb_id_find_next_upper(prio, sp->root, *sp->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		*sp->pid = 0;	/* ptr is valid now so that remove this hint as it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 				 * can become out of date quickly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		if (!*sp->pptr) {	/* we are at right end; rewind & go up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 			*sp->pptr = sp->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 			while ((*sp->pptr)->rb_left)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 				*sp->pptr = (*sp->pptr)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 			if (sp > stk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 				sp--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 				if (!*sp->pptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 					WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 					return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 				htb_next_rb_node(sp->pptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 			struct htb_class *cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 			struct htb_prio *clp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 			cl = rb_entry(*sp->pptr, struct htb_class, node[prio]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 			if (!cl->level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 				return cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 			clp = &cl->inner.clprio[prio];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 			(++sp)->root = clp->feed.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 			sp->pptr = &clp->ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 			sp->pid = &clp->last_ptr_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) /* dequeues packet at given priority and level; call only if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821)  * you are sure that there is active class at prio/level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) static struct sk_buff *htb_dequeue_tree(struct htb_sched *q, const int prio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 					const int level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	struct sk_buff *skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	struct htb_class *cl, *start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	struct htb_level *hlevel = &q->hlevel[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	struct htb_prio *hprio = &hlevel->hprio[prio];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	/* look initial class up in the row */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	start = cl = htb_lookup_leaf(hprio, prio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		if (unlikely(!cl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		/* class can be empty - it is unlikely but can be true if leaf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 		 * qdisc drops packets in enqueue routine or if someone used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		 * graft operation on the leaf since last dequeue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		 * simply deactivate and skip such class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		if (unlikely(cl->leaf.q->q.qlen == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 			struct htb_class *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 			htb_deactivate(q, cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 			/* row/level might become empty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 			if ((q->row_mask[level] & (1 << prio)) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 				return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 			next = htb_lookup_leaf(hprio, prio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 			if (cl == start)	/* fix start if we just deleted it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 				start = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 			cl = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 			goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		skb = cl->leaf.q->dequeue(cl->leaf.q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 		if (likely(skb != NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		qdisc_warn_nonwc("htb", cl->leaf.q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		htb_next_rb_node(level ? &cl->parent->inner.clprio[prio].ptr:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 					 &q->hlevel[0].hprio[prio].ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		cl = htb_lookup_leaf(hprio, prio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	} while (cl != start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	if (likely(skb != NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 		bstats_update(&cl->bstats, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		cl->leaf.deficit[level] -= qdisc_pkt_len(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		if (cl->leaf.deficit[level] < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 			cl->leaf.deficit[level] += cl->quantum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 			htb_next_rb_node(level ? &cl->parent->inner.clprio[prio].ptr :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 						 &q->hlevel[0].hprio[prio].ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		/* this used to be after charge_class but this constelation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		 * gives us slightly better performance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		if (!cl->leaf.q->q.qlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 			htb_deactivate(q, cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		htb_charge_class(q, cl, level, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	return skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) static struct sk_buff *htb_dequeue(struct Qdisc *sch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	struct htb_sched *q = qdisc_priv(sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	s64 next_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	unsigned long start_at;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	/* try to dequeue direct packets as high prio (!) to minimize cpu work */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	skb = __qdisc_dequeue_head(&q->direct_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	if (skb != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) ok:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 		qdisc_bstats_update(sch, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 		qdisc_qstats_backlog_dec(sch, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 		sch->q.qlen--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 		return skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	if (!sch->q.qlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		goto fin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	q->now = ktime_get_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	start_at = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	next_event = q->now + 5LLU * NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	for (level = 0; level < TC_HTB_MAXDEPTH; level++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		/* common case optimization - skip event handler quickly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		int m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		s64 event = q->near_ev_cache[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 		if (q->now >= event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 			event = htb_do_events(q, level, start_at);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 			if (!event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 				event = q->now + NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 			q->near_ev_cache[level] = event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 		if (next_event > event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 			next_event = event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 		m = ~q->row_mask[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 		while (m != (int)(-1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 			int prio = ffz(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 			m |= 1 << prio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 			skb = htb_dequeue_tree(q, prio, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 			if (likely(skb != NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 				goto ok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	if (likely(next_event > q->now))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		qdisc_watchdog_schedule_ns(&q->watchdog, next_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 		schedule_work(&q->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) fin:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	return skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) /* reset all classes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) /* always caled under BH & queue lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) static void htb_reset(struct Qdisc *sch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	struct htb_sched *q = qdisc_priv(sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	struct htb_class *cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	for (i = 0; i < q->clhash.hashsize; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 			if (cl->level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 				memset(&cl->inner, 0, sizeof(cl->inner));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 			else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 				if (cl->leaf.q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 					qdisc_reset(cl->leaf.q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 			cl->prio_activity = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 			cl->cmode = HTB_CAN_SEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	qdisc_watchdog_cancel(&q->watchdog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	__qdisc_reset_queue(&q->direct_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	sch->q.qlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	sch->qstats.backlog = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	memset(q->hlevel, 0, sizeof(q->hlevel));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	memset(q->row_mask, 0, sizeof(q->row_mask));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) static const struct nla_policy htb_policy[TCA_HTB_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	[TCA_HTB_PARMS]	= { .len = sizeof(struct tc_htb_opt) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	[TCA_HTB_INIT]	= { .len = sizeof(struct tc_htb_glob) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	[TCA_HTB_CTAB]	= { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	[TCA_HTB_RTAB]	= { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	[TCA_HTB_DIRECT_QLEN] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	[TCA_HTB_RATE64] = { .type = NLA_U64 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	[TCA_HTB_CEIL64] = { .type = NLA_U64 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) static void htb_work_func(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	struct htb_sched *q = container_of(work, struct htb_sched, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	struct Qdisc *sch = q->watchdog.qdisc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	__netif_schedule(qdisc_root(sch));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	rcu_read_unlock();
^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) static int htb_init(struct Qdisc *sch, struct nlattr *opt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		    struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	struct htb_sched *q = qdisc_priv(sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	struct nlattr *tb[TCA_HTB_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	struct tc_htb_glob *gopt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	qdisc_watchdog_init(&q->watchdog, sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	INIT_WORK(&q->work, htb_work_func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	if (!opt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	err = tcf_block_get(&q->block, &q->filter_list, sch, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	err = nla_parse_nested_deprecated(tb, TCA_HTB_MAX, opt, htb_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 					  NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	if (!tb[TCA_HTB_INIT])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	gopt = nla_data(tb[TCA_HTB_INIT]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	if (gopt->version != HTB_VER >> 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	err = qdisc_class_hash_init(&q->clhash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	qdisc_skb_head_init(&q->direct_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	if (tb[TCA_HTB_DIRECT_QLEN])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 		q->direct_qlen = nla_get_u32(tb[TCA_HTB_DIRECT_QLEN]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 		q->direct_qlen = qdisc_dev(sch)->tx_queue_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	if ((q->rate2quantum = gopt->rate2quantum) < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 		q->rate2quantum = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	q->defcls = gopt->defcls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) static int htb_dump(struct Qdisc *sch, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	struct htb_sched *q = qdisc_priv(sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	struct nlattr *nest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	struct tc_htb_glob gopt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	sch->qstats.overlimits = q->overlimits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	/* Its safe to not acquire qdisc lock. As we hold RTNL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	 * no change can happen on the qdisc parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	gopt.direct_pkts = q->direct_pkts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	gopt.version = HTB_VER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	gopt.rate2quantum = q->rate2quantum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	gopt.defcls = q->defcls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	gopt.debug = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	if (nest == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	if (nla_put(skb, TCA_HTB_INIT, sizeof(gopt), &gopt) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	    nla_put_u32(skb, TCA_HTB_DIRECT_QLEN, q->direct_qlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	return nla_nest_end(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	nla_nest_cancel(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) static int htb_dump_class(struct Qdisc *sch, unsigned long arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 			  struct sk_buff *skb, struct tcmsg *tcm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	struct htb_class *cl = (struct htb_class *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	struct nlattr *nest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	struct tc_htb_opt opt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	/* Its safe to not acquire qdisc lock. As we hold RTNL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	 * no change can happen on the class parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	tcm->tcm_parent = cl->parent ? cl->parent->common.classid : TC_H_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	tcm->tcm_handle = cl->common.classid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	if (!cl->level && cl->leaf.q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		tcm->tcm_info = cl->leaf.q->handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	if (nest == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	memset(&opt, 0, sizeof(opt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	psched_ratecfg_getrate(&opt.rate, &cl->rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	opt.buffer = PSCHED_NS2TICKS(cl->buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	psched_ratecfg_getrate(&opt.ceil, &cl->ceil);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	opt.cbuffer = PSCHED_NS2TICKS(cl->cbuffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	opt.quantum = cl->quantum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	opt.prio = cl->prio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	opt.level = cl->level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	if (nla_put(skb, TCA_HTB_PARMS, sizeof(opt), &opt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	if ((cl->rate.rate_bytes_ps >= (1ULL << 32)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	    nla_put_u64_64bit(skb, TCA_HTB_RATE64, cl->rate.rate_bytes_ps,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 			      TCA_HTB_PAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	if ((cl->ceil.rate_bytes_ps >= (1ULL << 32)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	    nla_put_u64_64bit(skb, TCA_HTB_CEIL64, cl->ceil.rate_bytes_ps,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 			      TCA_HTB_PAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	return nla_nest_end(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	nla_nest_cancel(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) htb_dump_class_stats(struct Qdisc *sch, unsigned long arg, struct gnet_dump *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	struct htb_class *cl = (struct htb_class *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	struct gnet_stats_queue qs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 		.drops = cl->drops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 		.overlimits = cl->overlimits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	__u32 qlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	if (!cl->level && cl->leaf.q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 		qdisc_qstats_qlen_backlog(cl->leaf.q, &qlen, &qs.backlog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	cl->xstats.tokens = clamp_t(s64, PSCHED_NS2TICKS(cl->tokens),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 				    INT_MIN, INT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	cl->xstats.ctokens = clamp_t(s64, PSCHED_NS2TICKS(cl->ctokens),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 				     INT_MIN, INT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 				  d, NULL, &cl->bstats) < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	    gnet_stats_copy_rate_est(d, &cl->rate_est) < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	    gnet_stats_copy_queue(d, NULL, &qs, qlen) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	return gnet_stats_copy_app(d, &cl->xstats, sizeof(cl->xstats));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) static int htb_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 		     struct Qdisc **old, struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	struct htb_class *cl = (struct htb_class *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	if (cl->level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	if (new == NULL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	    (new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 				     cl->common.classid, extack)) == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 		return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	*old = qdisc_replace(sch, new, &cl->leaf.q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) static struct Qdisc *htb_leaf(struct Qdisc *sch, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	struct htb_class *cl = (struct htb_class *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	return !cl->level ? cl->leaf.q : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) static void htb_qlen_notify(struct Qdisc *sch, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	struct htb_class *cl = (struct htb_class *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	htb_deactivate(qdisc_priv(sch), cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) static inline int htb_parent_last_child(struct htb_class *cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	if (!cl->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 		/* the root class */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	if (cl->parent->children > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 		/* not the last child */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) static void htb_parent_to_leaf(struct htb_sched *q, struct htb_class *cl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 			       struct Qdisc *new_q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	struct htb_class *parent = cl->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	WARN_ON(cl->level || !cl->leaf.q || cl->prio_activity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	if (parent->cmode != HTB_CAN_SEND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		htb_safe_rb_erase(&parent->pq_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 				  &q->hlevel[parent->level].wait_pq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	parent->level = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	memset(&parent->inner, 0, sizeof(parent->inner));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	parent->leaf.q = new_q ? new_q : &noop_qdisc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	parent->tokens = parent->buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	parent->ctokens = parent->cbuffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	parent->t_c = ktime_get_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	parent->cmode = HTB_CAN_SEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) static void htb_destroy_class(struct Qdisc *sch, struct htb_class *cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	if (!cl->level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 		WARN_ON(!cl->leaf.q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 		qdisc_put(cl->leaf.q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	gen_kill_estimator(&cl->rate_est);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 	tcf_block_put(cl->block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	kfree(cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) static void htb_destroy(struct Qdisc *sch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	struct htb_sched *q = qdisc_priv(sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	struct hlist_node *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	struct htb_class *cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	cancel_work_sync(&q->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	qdisc_watchdog_cancel(&q->watchdog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	/* This line used to be after htb_destroy_class call below
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	 * and surprisingly it worked in 2.4. But it must precede it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	 * because filter need its target class alive to be able to call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	 * unbind_filter on it (without Oops).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	tcf_block_put(q->block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	for (i = 0; i < q->clhash.hashsize; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 		hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 			tcf_block_put(cl->block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 			cl->block = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	for (i = 0; i < q->clhash.hashsize; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 		hlist_for_each_entry_safe(cl, next, &q->clhash.hash[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 					  common.hnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 			htb_destroy_class(sch, cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	qdisc_class_hash_destroy(&q->clhash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	__qdisc_reset_queue(&q->direct_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) static int htb_delete(struct Qdisc *sch, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	struct htb_sched *q = qdisc_priv(sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	struct htb_class *cl = (struct htb_class *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	struct Qdisc *new_q = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	int last_child = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	/* TODO: why don't allow to delete subtree ? references ? does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	 * tc subsys guarantee us that in htb_destroy it holds no class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	 * refs so that we can remove children safely there ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	if (cl->children || cl->filter_cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	if (!cl->level && htb_parent_last_child(cl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 		new_q = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 					  cl->parent->common.classid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 					  NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 		last_child = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	sch_tree_lock(sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	if (!cl->level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 		qdisc_purge_queue(cl->leaf.q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	/* delete from hash and active; remainder in destroy_class */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	qdisc_class_hash_remove(&q->clhash, &cl->common);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	if (cl->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 		cl->parent->children--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	if (cl->prio_activity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		htb_deactivate(q, cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	if (cl->cmode != HTB_CAN_SEND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 		htb_safe_rb_erase(&cl->pq_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 				  &q->hlevel[cl->level].wait_pq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	if (last_child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 		htb_parent_to_leaf(q, cl, new_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	sch_tree_unlock(sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	htb_destroy_class(sch, cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) static int htb_change_class(struct Qdisc *sch, u32 classid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 			    u32 parentid, struct nlattr **tca,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 			    unsigned long *arg, struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	struct htb_sched *q = qdisc_priv(sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	struct htb_class *cl = (struct htb_class *)*arg, *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	struct nlattr *opt = tca[TCA_OPTIONS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	struct nlattr *tb[TCA_HTB_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	struct Qdisc *parent_qdisc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	struct tc_htb_opt *hopt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	u64 rate64, ceil64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	int warn = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	/* extract all subattrs from opt attr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	if (!opt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 		goto failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	err = nla_parse_nested_deprecated(tb, TCA_HTB_MAX, opt, htb_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 					  NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 		goto failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 	err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	if (tb[TCA_HTB_PARMS] == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 		goto failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	parent = parentid == TC_H_ROOT ? NULL : htb_find(parentid, sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	hopt = nla_data(tb[TCA_HTB_PARMS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	if (!hopt->rate.rate || !hopt->ceil.rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 		goto failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 	/* Keeping backward compatible with rate_table based iproute2 tc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	if (hopt->rate.linklayer == TC_LINKLAYER_UNAWARE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 		qdisc_put_rtab(qdisc_get_rtab(&hopt->rate, tb[TCA_HTB_RTAB],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 					      NULL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	if (hopt->ceil.linklayer == TC_LINKLAYER_UNAWARE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 		qdisc_put_rtab(qdisc_get_rtab(&hopt->ceil, tb[TCA_HTB_CTAB],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 					      NULL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	if (!cl) {		/* new class */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 		struct Qdisc *new_q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 		int prio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 		struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 			struct nlattr		nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 			struct gnet_estimator	opt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 		} est = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 			.nla = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 				.nla_len	= nla_attr_size(sizeof(est.opt)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 				.nla_type	= TCA_RATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 			},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 			.opt = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 				/* 4s interval, 16s averaging constant */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 				.interval	= 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 				.ewma_log	= 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 			},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 		/* check for valid classid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 		if (!classid || TC_H_MAJ(classid ^ sch->handle) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 		    htb_find(classid, sch))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 			goto failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 		/* check maximal depth */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 		if (parent && parent->parent && parent->parent->level < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 			pr_err("htb: tree is too deep\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 			goto failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 		err = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 		cl = kzalloc(sizeof(*cl), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 		if (!cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 			goto failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 		err = tcf_block_get(&cl->block, &cl->filter_list, sch, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 			kfree(cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 			goto failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 		if (htb_rate_est || tca[TCA_RATE]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 			err = gen_new_estimator(&cl->bstats, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 						&cl->rate_est,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 						NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 						qdisc_root_sleeping_running(sch),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 						tca[TCA_RATE] ? : &est.nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 			if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 				tcf_block_put(cl->block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 				kfree(cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 				goto failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 		cl->children = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 		RB_CLEAR_NODE(&cl->pq_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 		for (prio = 0; prio < TC_HTB_NUMPRIO; prio++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 			RB_CLEAR_NODE(&cl->node[prio]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 		/* create leaf qdisc early because it uses kmalloc(GFP_KERNEL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 		 * so that can't be used inside of sch_tree_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 		 * -- thanks to Karlis Peisenieks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 		new_q = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 					  classid, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 		sch_tree_lock(sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 		if (parent && !parent->level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 			/* turn parent into inner node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 			qdisc_purge_queue(parent->leaf.q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 			parent_qdisc = parent->leaf.q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 			if (parent->prio_activity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 				htb_deactivate(q, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 			/* remove from evt list because of level change */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 			if (parent->cmode != HTB_CAN_SEND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 				htb_safe_rb_erase(&parent->pq_node, &q->hlevel[0].wait_pq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 				parent->cmode = HTB_CAN_SEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 			parent->level = (parent->parent ? parent->parent->level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 					 : TC_HTB_MAXDEPTH) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 			memset(&parent->inner, 0, sizeof(parent->inner));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 		/* leaf (we) needs elementary qdisc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 		cl->leaf.q = new_q ? new_q : &noop_qdisc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 		cl->common.classid = classid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 		cl->parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 		/* set class to be in HTB_CAN_SEND state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 		cl->tokens = PSCHED_TICKS2NS(hopt->buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		cl->ctokens = PSCHED_TICKS2NS(hopt->cbuffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 		cl->mbuffer = 60ULL * NSEC_PER_SEC;	/* 1min */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 		cl->t_c = ktime_get_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 		cl->cmode = HTB_CAN_SEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 		/* attach to the hash list and parent's family */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 		qdisc_class_hash_insert(&q->clhash, &cl->common);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 		if (parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 			parent->children++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 		if (cl->leaf.q != &noop_qdisc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 			qdisc_hash_add(cl->leaf.q, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 		if (tca[TCA_RATE]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 			err = gen_replace_estimator(&cl->bstats, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 						    &cl->rate_est,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 						    NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 						    qdisc_root_sleeping_running(sch),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 						    tca[TCA_RATE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 				return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 		sch_tree_lock(sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	rate64 = tb[TCA_HTB_RATE64] ? nla_get_u64(tb[TCA_HTB_RATE64]) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	ceil64 = tb[TCA_HTB_CEIL64] ? nla_get_u64(tb[TCA_HTB_CEIL64]) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	psched_ratecfg_precompute(&cl->rate, &hopt->rate, rate64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	psched_ratecfg_precompute(&cl->ceil, &hopt->ceil, ceil64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	/* it used to be a nasty bug here, we have to check that node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	 * is really leaf before changing cl->leaf !
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	if (!cl->level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 		u64 quantum = cl->rate.rate_bytes_ps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 		do_div(quantum, q->rate2quantum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 		cl->quantum = min_t(u64, quantum, INT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 		if (!hopt->quantum && cl->quantum < 1000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 			warn = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 			cl->quantum = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 		if (!hopt->quantum && cl->quantum > 200000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 			warn = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 			cl->quantum = 200000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 		if (hopt->quantum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 			cl->quantum = hopt->quantum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 		if ((cl->prio = hopt->prio) >= TC_HTB_NUMPRIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 			cl->prio = TC_HTB_NUMPRIO - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	cl->buffer = PSCHED_TICKS2NS(hopt->buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	cl->cbuffer = PSCHED_TICKS2NS(hopt->cbuffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	sch_tree_unlock(sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	qdisc_put(parent_qdisc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	if (warn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 		pr_warn("HTB: quantum of class %X is %s. Consider r2q change.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 			    cl->common.classid, (warn == -1 ? "small" : "big"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	qdisc_class_hash_grow(sch, &q->clhash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	*arg = (unsigned long)cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) static struct tcf_block *htb_tcf_block(struct Qdisc *sch, unsigned long arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 				       struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 	struct htb_sched *q = qdisc_priv(sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	struct htb_class *cl = (struct htb_class *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 	return cl ? cl->block : q->block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) static unsigned long htb_bind_filter(struct Qdisc *sch, unsigned long parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 				     u32 classid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	struct htb_class *cl = htb_find(classid, sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	/*if (cl && !cl->level) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	 * The line above used to be there to prevent attaching filters to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	 * leaves. But at least tc_index filter uses this just to get class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 	 * for other reasons so that we have to allow for it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	 * ----
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	 * 19.6.2002 As Werner explained it is ok - bind filter is just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	 * another way to "lock" the class - unlike "get" this lock can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	 * be broken by class during destroy IIUC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	if (cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 		cl->filter_cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	return (unsigned long)cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) static void htb_unbind_filter(struct Qdisc *sch, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	struct htb_class *cl = (struct htb_class *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 	if (cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 		cl->filter_cnt--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) static void htb_walk(struct Qdisc *sch, struct qdisc_walker *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	struct htb_sched *q = qdisc_priv(sch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	struct htb_class *cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	if (arg->stop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	for (i = 0; i < q->clhash.hashsize; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 		hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 			if (arg->count < arg->skip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 				arg->count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 			if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 				arg->stop = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 				return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 			arg->count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) static const struct Qdisc_class_ops htb_class_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	.graft		=	htb_graft,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 	.leaf		=	htb_leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	.qlen_notify	=	htb_qlen_notify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 	.find		=	htb_search,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	.change		=	htb_change_class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	.delete		=	htb_delete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	.walk		=	htb_walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	.tcf_block	=	htb_tcf_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	.bind_tcf	=	htb_bind_filter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 	.unbind_tcf	=	htb_unbind_filter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	.dump		=	htb_dump_class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 	.dump_stats	=	htb_dump_class_stats,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) static struct Qdisc_ops htb_qdisc_ops __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	.cl_ops		=	&htb_class_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	.id		=	"htb",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	.priv_size	=	sizeof(struct htb_sched),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	.enqueue	=	htb_enqueue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	.dequeue	=	htb_dequeue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 	.peek		=	qdisc_peek_dequeued,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 	.init		=	htb_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	.reset		=	htb_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 	.destroy	=	htb_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	.dump		=	htb_dump,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	.owner		=	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) static int __init htb_module_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 	return register_qdisc(&htb_qdisc_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) static void __exit htb_module_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	unregister_qdisc(&htb_qdisc_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) module_init(htb_module_init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) module_exit(htb_module_exit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) MODULE_LICENSE("GPL");