⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sch_htb.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * net/sched/sch_htb.c	Hierarchical token bucket, feed tree version * *		This program is free software; you can redistribute it and/or *		modify it under the terms of the GNU General Public License *		as published by the Free Software Foundation; either version *		2 of the License, or (at your option) any later version. * * Authors:	Martin Devera, <devik@cdi.cz> * * Credits (in time order) for older HTB versions: *              Stef Coene <stef.coene@docum.org> *			HTB support at LARTC mailing list *		Ondrej Kraus, <krauso@barr.cz> *			found missing INIT_QDISC(htb) *		Vladimir Smelhaus, Aamer Akhter, Bert Hubert *			helped a lot to locate nasty class stall bug *		Andi Kleen, Jamal Hadi, Bert Hubert *			code review and helpful comments on shaping *		Tomasz Wrona, <tw@eter.tym.pl> *			created test case so that I was able to fix nasty bug *		Wilfried Weissmann *			spotted bug in dequeue code and helped with fix *		Jiri Fojtasek *			fixed requeue routine *		and many others. thanks. * * $Id: sch_htb.c,v 1.25 2003/12/07 11:08:25 devik Exp devik $ */#include <linux/module.h>#include <linux/types.h>#include <linux/kernel.h>#include <linux/string.h>#include <linux/errno.h>#include <linux/skbuff.h>#include <linux/list.h>#include <linux/compiler.h>#include <linux/rbtree.h>#include <net/netlink.h>#include <net/pkt_sched.h>/* HTB algorithm.    Author: devik@cdi.cz    ========================================================================    HTB is like TBF with multiple classes. It is also similar to CBQ because    it allows to assign priority to each class in hierarchy.    In fact it is another implementation of Floyd's formal sharing.    Levels:    Each class is assigned level. Leaf has ALWAYS level 0 and root    classes have level TC_HTB_MAXDEPTH-1. Interior nodes has level    one less than their parent.*/#define HTB_HSIZE 16		/* classid hash size */#define HTB_HYSTERESIS 1	/* whether to use mode hysteresis for speedup */#define HTB_VER 0x30011		/* major must be matched with number suplied by TC as version */#if HTB_VER >> 16 != TC_HTB_PROTOVER#error "Mismatched sch_htb.c and pkt_sch.h"#endif/* used internaly to keep status of single class */enum htb_cmode {	HTB_CANT_SEND,		/* class can't send and can't borrow */	HTB_MAY_BORROW,		/* class can't send but may borrow */	HTB_CAN_SEND		/* class can send */};/* interior & leaf nodes; props specific to leaves are marked L: */struct htb_class {	/* general class parameters */	u32 classid;	struct gnet_stats_basic bstats;	struct gnet_stats_queue qstats;	struct gnet_stats_rate_est rate_est;	struct tc_htb_xstats xstats;	/* our special stats */	int refcnt;		/* usage count of this class */	/* topology */	int level;		/* our level (see above) */	struct htb_class *parent;	/* parent class */	struct hlist_node hlist;	/* classid hash list item */	struct list_head sibling;	/* sibling list item */	struct list_head children;	/* children list */	union {		struct htb_class_leaf {			struct Qdisc *q;			int prio;			int aprio;			int quantum;			int deficit[TC_HTB_MAXDEPTH];			struct list_head drop_list;		} leaf;		struct htb_class_inner {			struct rb_root feed[TC_HTB_NUMPRIO];	/* feed trees */			struct rb_node *ptr[TC_HTB_NUMPRIO];	/* current class ptr */			/* When class changes from state 1->2 and disconnects from			   parent's feed then we lost ptr value and start from the			   first child again. Here we store classid of the			   last valid ptr (used when ptr is NULL). */			u32 last_ptr_id[TC_HTB_NUMPRIO];		} inner;	} un;	struct rb_node node[TC_HTB_NUMPRIO];	/* node for self or feed tree */	struct rb_node pq_node;	/* node for event queue */	psched_time_t pq_key;	int prio_activity;	/* for which prios are we active */	enum htb_cmode cmode;	/* current mode of the class */	/* class attached filters */	struct tcf_proto *filter_list;	int filter_cnt;	int warned;		/* only one warning about non work conserving .. */	/* token bucket parameters */	struct qdisc_rate_table *rate;	/* rate table of the class itself */	struct qdisc_rate_table *ceil;	/* ceiling rate (limits borrows too) */	long buffer, cbuffer;	/* token bucket depth/rate */	psched_tdiff_t mbuffer;	/* max wait time */	long tokens, ctokens;	/* current number of tokens */	psched_time_t t_c;	/* checkpoint time */	int prio;		/* For parent to leaf return possible here */	int quantum;		/* we do backup. Finally full replacement  */				/* of un.leaf originals should be done. */};static inline long L2T(struct htb_class *cl, struct qdisc_rate_table *rate,			   int size){	long result = qdisc_l2t(rate, size);	return result;}struct htb_sched {	struct list_head root;	/* root classes list */	struct hlist_head hash[HTB_HSIZE];	/* hashed by classid */	struct list_head drops[TC_HTB_NUMPRIO];/* active leaves (for drops) */	/* self list - roots of self generating tree */	struct rb_root row[TC_HTB_MAXDEPTH][TC_HTB_NUMPRIO];	int row_mask[TC_HTB_MAXDEPTH];	struct rb_node *ptr[TC_HTB_MAXDEPTH][TC_HTB_NUMPRIO];	u32 last_ptr_id[TC_HTB_MAXDEPTH][TC_HTB_NUMPRIO];	/* self wait list - roots of wait PQs per row */	struct rb_root wait_pq[TC_HTB_MAXDEPTH];	/* time of nearest event per level (row) */	psched_time_t near_ev_cache[TC_HTB_MAXDEPTH];	/* whether we hit non-work conserving class during this dequeue; we use */	int nwc_hit;		/* this to disable mindelay complaint in dequeue */	int defcls;		/* class where unclassified flows go to */	/* filters for qdisc itself */	struct tcf_proto *filter_list;	int filter_cnt;	int rate2quantum;	/* quant = rate / rate2quantum */	psched_time_t now;	/* cached dequeue time */	struct qdisc_watchdog watchdog;	/* non shaped skbs; let them go directly thru */	struct sk_buff_head direct_queue;	int direct_qlen;	/* max qlen of above */	long direct_pkts;};/* compute hash of size HTB_HSIZE for given handle */static inline int htb_hash(u32 h){#if HTB_HSIZE != 16#error "Declare new hash for your HTB_HSIZE"#endif	h ^= h >> 8;		/* stolen from cbq_hash */	h ^= h >> 4;	return h & 0xf;}/* find class in global hash table using given handle */static inline struct htb_class *htb_find(u32 handle, struct Qdisc *sch){	struct htb_sched *q = qdisc_priv(sch);	struct hlist_node *p;	struct htb_class *cl;	if (TC_H_MAJ(handle) != sch->handle)		return NULL;	hlist_for_each_entry(cl, p, q->hash + htb_hash(handle), hlist) {		if (cl->classid == handle)			return cl;	}	return NULL;}/** * htb_classify - classify a packet into class * * It returns NULL if the packet should be dropped or -1 if the packet * should be passed directly thru. In all other cases leaf class is returned. * We allow direct class selection by classid in priority. The we examine * filters in qdisc and in inner nodes (if higher filter points to the inner * node). If we end up with classid MAJOR:0 we enqueue the skb into special * internal fifo (direct). These packets then go directly thru. If we still * have no valid leaf we try to use MAJOR:default leaf. It still unsuccessfull * then finish and return direct queue. */#define HTB_DIRECT (struct htb_class*)-1static inline u32 htb_classid(struct htb_class *cl){	return (cl && cl != HTB_DIRECT) ? cl->classid : TC_H_UNSPEC;}static struct htb_class *htb_classify(struct sk_buff *skb, struct Qdisc *sch,				      int *qerr){	struct htb_sched *q = qdisc_priv(sch);	struct htb_class *cl;	struct tcf_result res;	struct tcf_proto *tcf;	int result;	/* allow to select class by setting skb->priority to valid classid;	   note that nfmark can be used too by attaching filter fw with no	   rules in it */	if (skb->priority == sch->handle)		return HTB_DIRECT;	/* X:0 (direct flow) selected */	if ((cl = htb_find(skb->priority, sch)) != NULL && cl->level == 0)		return cl;	*qerr = NET_XMIT_BYPASS;	tcf = q->filter_list;	while (tcf && (result = tc_classify(skb, tcf, &res)) >= 0) {#ifdef CONFIG_NET_CLS_ACT		switch (result) {		case TC_ACT_QUEUED:		case TC_ACT_STOLEN:			*qerr = NET_XMIT_SUCCESS;		case TC_ACT_SHOT:			return NULL;		}#endif		if ((cl = (void *)res.class) == NULL) {			if (res.classid == sch->handle)				return HTB_DIRECT;	/* X:0 (direct flow) */			if ((cl = htb_find(res.classid, sch)) == NULL)				break;	/* filter selected invalid classid */		}		if (!cl->level)			return cl;	/* we hit leaf; return it */		/* we have got inner class; apply inner filter chain */		tcf = cl->filter_list;	}	/* classification failed; try to use default class */	cl = htb_find(TC_H_MAKE(TC_H_MAJ(sch->handle), q->defcls), sch);	if (!cl || cl->level)		return HTB_DIRECT;	/* bad default .. this is safe bet */	return cl;}/** * htb_add_to_id_tree - adds class to the round robin list * * Routine adds class to the list (actually tree) sorted by classid. * Make sure that class is not already on such list for given prio. */static void htb_add_to_id_tree(struct rb_root *root,			       struct htb_class *cl, int prio){	struct rb_node **p = &root->rb_node, *parent = NULL;	while (*p) {		struct htb_class *c;		parent = *p;		c = rb_entry(parent, struct htb_class, node[prio]);		if (cl->classid > c->classid)			p = &parent->rb_right;		else			p = &parent->rb_left;	}	rb_link_node(&cl->node[prio], parent, p);	rb_insert_color(&cl->node[prio], root);}/** * htb_add_to_wait_tree - adds class to the event queue with delay * * The class is added to priority event queue to indicate that class will * change its mode in cl->pq_key microseconds. Make sure that class is not * already in the queue. */static void htb_add_to_wait_tree(struct htb_sched *q,				 struct htb_class *cl, long delay){	struct rb_node **p = &q->wait_pq[cl->level].rb_node, *parent = NULL;	cl->pq_key = q->now + delay;	if (cl->pq_key == q->now)		cl->pq_key++;	/* update the nearest event cache */	if (q->near_ev_cache[cl->level] > cl->pq_key)		q->near_ev_cache[cl->level] = cl->pq_key;	while (*p) {		struct htb_class *c;		parent = *p;		c = rb_entry(parent, struct htb_class, pq_node);		if (cl->pq_key >= c->pq_key)			p = &parent->rb_right;		else			p = &parent->rb_left;	}	rb_link_node(&cl->pq_node, parent, p);	rb_insert_color(&cl->pq_node, &q->wait_pq[cl->level]);}/** * htb_next_rb_node - finds next node in binary tree * * When we are past last key we return NULL. * Average complexity is 2 steps per call. */static inline void htb_next_rb_node(struct rb_node **n){	*n = rb_next(*n);}/** * htb_add_class_to_row - add class to its row * * The class is added to row at priorities marked in mask. * It does nothing if mask == 0. */static inline void htb_add_class_to_row(struct htb_sched *q,					struct htb_class *cl, int mask){	q->row_mask[cl->level] |= mask;	while (mask) {		int prio = ffz(~mask);		mask &= ~(1 << prio);		htb_add_to_id_tree(q->row[cl->level] + prio, cl, prio);	}}/* If this triggers, it is a bug in this code, but it need not be fatal */static void htb_safe_rb_erase(struct rb_node *rb, struct rb_root *root){	if (RB_EMPTY_NODE(rb)) {		WARN_ON(1);	} else {		rb_erase(rb, root);		RB_CLEAR_NODE(rb);	}}/** * htb_remove_class_from_row - removes class from its row * * The class is removed from row at priorities marked in mask. * It does nothing if mask == 0. */static inline void htb_remove_class_from_row(struct htb_sched *q,						 struct htb_class *cl, int mask){	int m = 0;	while (mask) {		int prio = ffz(~mask);		mask &= ~(1 << prio);		if (q->ptr[cl->level][prio] == cl->node + prio)			htb_next_rb_node(q->ptr[cl->level] + prio);		htb_safe_rb_erase(cl->node + prio, q->row[cl->level] + prio);		if (!q->row[cl->level][prio].rb_node)			m |= 1 << prio;	}	q->row_mask[cl->level] &= ~m;}/** * htb_activate_prios - creates active classe's feed chain * * The class is connected to ancestors and/or appropriate rows * for priorities it is participating on. cl->cmode must be new * (activated) mode. It does nothing if cl->prio_activity == 0. */static void htb_activate_prios(struct htb_sched *q, struct htb_class *cl){	struct htb_class *p = cl->parent;	long m, mask = cl->prio_activity;	while (cl->cmode == HTB_MAY_BORROW && p && mask) {		m = mask;		while (m) {			int prio = ffz(~m);			m &= ~(1 << prio);			if (p->un.inner.feed[prio].rb_node)				/* parent already has its feed in use so that				   reset bit in mask as parent is already ok */				mask &= ~(1 << prio);			htb_add_to_id_tree(p->un.inner.feed + prio, cl, prio);		}		p->prio_activity |= mask;		cl = p;		p = cl->parent;	}	if (cl->cmode == HTB_CAN_SEND && mask)		htb_add_class_to_row(q, cl, mask);}/** * htb_deactivate_prios - remove class from feed chain * * cl->cmode must represent old mode (before deactivation). It does * nothing if cl->prio_activity == 0. Class is removed from all feed * chains and rows. */static void htb_deactivate_prios(struct htb_sched *q, struct htb_class *cl){	struct htb_class *p = cl->parent;	long m, mask = cl->prio_activity;	while (cl->cmode == HTB_MAY_BORROW && p && mask) {		m = mask;		mask = 0;		while (m) {			int prio = ffz(~m);			m &= ~(1 << prio);			if (p->un.inner.ptr[prio] == cl->node + prio) {				/* we are removing child which is pointed to from				   parent feed - forget the pointer but remember				   classid */				p->un.inner.last_ptr_id[prio] = cl->classid;				p->un.inner.ptr[prio] = NULL;			}			htb_safe_rb_erase(cl->node + prio, p->un.inner.feed + prio);			if (!p->un.inner.feed[prio].rb_node)				mask |= 1 << prio;		}		p->prio_activity &= ~mask;		cl = p;		p = cl->parent;	}	if (cl->cmode == HTB_CAN_SEND && mask)		htb_remove_class_from_row(q, cl, mask);}#if HTB_HYSTERESISstatic inline long htb_lowater(const struct htb_class *cl){	return cl->cmode != HTB_CANT_SEND ? -cl->cbuffer : 0;}static inline long htb_hiwater(const struct htb_class *cl){	return cl->cmode == HTB_CAN_SEND ? -cl->buffer : 0;}#else#define htb_lowater(cl)	(0)#define htb_hiwater(cl)	(0)#endif/** * htb_class_mode - computes and returns current class mode * * It computes cl's mode at time cl->t_c+diff and returns it. If mode * is not HTB_CAN_SEND then cl->pq_key is updated to time difference * from now to time when cl will change its state. * Also it is worth to note that class mode doesn't change simply * at cl->{c,}tokens == 0 but there can rather be hysteresis of * 0 .. -cl->{c,}buffer range. It is meant to limit number of * mode transitions per time unit. The speed gain is about 1/6. */static inline enum htb_cmodehtb_class_mode(struct htb_class *cl, long *diff){	long toks;	if ((toks = (cl->ctokens + *diff)) < htb_lowater(cl)) {		*diff = -toks;		return HTB_CANT_SEND;	}	if ((toks = (cl->tokens + *diff)) >= htb_hiwater(cl))		return HTB_CAN_SEND;	*diff = -toks;	return HTB_MAY_BORROW;}/** * htb_change_class_mode - changes classe's mode * * This should be the only way how to change classe's mode under normal * cirsumstances. Routine will update feed lists linkage, change mode * and add class to the wait event queue if appropriate. New mode should * be different from old one and cl->pq_key has to be valid if changing * to mode other than HTB_CAN_SEND (see htb_add_to_wait_tree). */static voidhtb_change_class_mode(struct htb_sched *q, struct htb_class *cl, long *diff){	enum htb_cmode new_mode = htb_class_mode(cl, diff);	if (new_mode == cl->cmode)		return;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -