📄 sch_htb.c
字号:
/* vim: ts=8 sw=8 * 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/config.h>#include <linux/module.h>#include <asm/uaccess.h>#include <asm/system.h>#include <asm/bitops.h>#include <linux/types.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/string.h>#include <linux/mm.h>#include <linux/socket.h>#include <linux/sockios.h>#include <linux/in.h>#include <linux/errno.h>#include <linux/interrupt.h>#include <linux/if_ether.h>#include <linux/inet.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/notifier.h>#include <net/ip.h>#include <net/route.h>#include <linux/skbuff.h>#include <linux/list.h>#include <linux/compiler.h>#include <net/sock.h>#include <net/pkt_sched.h>#include <linux/rbtree.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_EWMAC 2 /* rate average over HTB_EWMAC*HTB_HSIZE sec */#define HTB_DEBUG 1 /* compile debugging support (activated by tc tool) */#define HTB_RATECM 1 /* whether to use rate computer */#define HTB_HYSTERESIS 1/* whether to use mode hysteresis for speedup */#define HTB_QLOCK(S) spin_lock_bh(&(S)->dev->queue_lock)#define HTB_QUNLOCK(S) spin_unlock_bh(&(S)->dev->queue_lock)#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/* debugging support; S is subsystem, these are defined: 0 - netlink messages 1 - enqueue 2 - drop & requeue 3 - dequeue main 4 - dequeue one prio DRR part 5 - dequeue class accounting 6 - class overlimit status computation 7 - hint tree 8 - event queue 10 - rate estimator 11 - classifier 12 - fast dequeue cache L is level; 0 = none, 1 = basic info, 2 = detailed, 3 = full q->debug uint32 contains 16 2-bit fields one for subsystem starting from LSB */#ifdef HTB_DEBUG#define HTB_DBG_COND(S,L) (((q->debug>>(2*S))&3) >= L)#define HTB_DBG(S,L,FMT,ARG...) if (HTB_DBG_COND(S,L)) \ printk(KERN_DEBUG FMT,##ARG)#define HTB_CHCL(cl) BUG_TRAP((cl)->magic == HTB_CMAGIC)#define HTB_PASSQ q,#define HTB_ARGQ struct htb_sched *q,#define static#undef __inline__#define __inline__#undef inline#define inline#define HTB_CMAGIC 0xFEFAFEF1#define htb_safe_rb_erase(N,R) do { BUG_TRAP((N)->rb_color != -1); \ if ((N)->rb_color == -1) break; \ rb_erase(N,R); \ (N)->rb_color = -1; } while (0)#else#define HTB_DBG_COND(S,L) (0)#define HTB_DBG(S,L,FMT,ARG...)#define HTB_PASSQ#define HTB_ARGQ#define HTB_CHCL(cl)#define htb_safe_rb_erase(N,R) rb_erase(N,R)#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{#ifdef HTB_DEBUG unsigned magic;#endif /* general class parameters */ u32 classid; struct tc_stats stats; /* generic stats */ spinlock_t *stats_lock; struct tc_htb_xstats xstats;/* our special stats */ int refcnt; /* usage count of this class */#ifdef HTB_RATECM /* rate measurement counters */ unsigned long rate_bytes,sum_bytes; unsigned long rate_packets,sum_packets;#endif /* topology */ int level; /* our level (see above) */ struct htb_class *parent; /* parent class */ struct list_head 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 */ unsigned long pq_key; /* the same type as jiffies global */ 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 */ long mbuffer; /* max wait time */ long tokens,ctokens; /* current number of tokens */ psched_time_t t_c; /* checkpoint time */};/* TODO: maybe compute rate when size is too large .. or drop ? */static __inline__ long L2T(struct htb_class *cl,struct qdisc_rate_table *rate, int size){ int slot = size >> rate->rate.cell_log; if (slot > 255) { cl->xstats.giants++; slot = 255; } return rate->data[slot];}struct htb_sched{ struct list_head root; /* root classes list */ struct list_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) */ unsigned long near_ev_cache[TC_HTB_MAXDEPTH]; /* cached value of jiffies in dequeue */ unsigned long jiffies; /* 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 */ u32 debug; /* subsystem debug levels */ /* 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 timer_list timer; /* send delay timer */#ifdef HTB_RATECM struct timer_list rttim; /* rate computer timer */ int recmp_bucket; /* which hash bucket to recompute next */#endif /* 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 list_head *p; if (TC_H_MAJ(handle) != sch->handle) return NULL; list_for_each (p,q->hash+htb_hash(handle)) { struct htb_class *cl = list_entry(p,struct htb_class,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 *qres){ 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; tcf = q->filter_list; while (tcf && (result = tc_classify(skb, tcf, &res)) >= 0) {#ifdef CONFIG_NET_CLS_ACT int terminal = 0; switch (result) { case TC_ACT_SHOT: /* Stop and kfree */ *qres = NET_XMIT_DROP; terminal = 1; break; case TC_ACT_QUEUED: case TC_ACT_STOLEN: terminal = 1; break; case TC_ACT_RECLASSIFY: /* Things look good */ case TC_ACT_OK: case TC_ACT_UNSPEC: default: break; } if (terminal) { kfree_skb(skb); return NULL; }#else#ifdef CONFIG_NET_CLS_POLICE if (result == TC_POLICE_SHOT) return NULL;#endif#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;}#ifdef HTB_DEBUGstatic void htb_next_rb_node(struct rb_node **n);#define HTB_DUMTREE(root,memb) if(root) { \ struct rb_node *n = (root)->rb_node; \ while (n->rb_left) n = n->rb_left; \ while (n) { \ struct htb_class *cl = rb_entry(n, struct htb_class, memb); \ printk(" %x",cl->classid); htb_next_rb_node (&n); \ } }static void htb_debug_dump (struct htb_sched *q){ int i,p; printk(KERN_DEBUG "htb*g j=%lu lj=%lu\n",jiffies,q->jiffies); /* rows */ for (i=TC_HTB_MAXDEPTH-1;i>=0;i--) { printk(KERN_DEBUG "htb*r%d m=%x",i,q->row_mask[i]); for (p=0;p<TC_HTB_NUMPRIO;p++) { if (!q->row[i][p].rb_node) continue; printk(" p%d:",p); HTB_DUMTREE(q->row[i]+p,node[p]); } printk("\n"); } /* classes */ for (i = 0; i < HTB_HSIZE; i++) { struct list_head *l; list_for_each (l,q->hash+i) { struct htb_class *cl = list_entry(l,struct htb_class,hlist); long diff = PSCHED_TDIFF_SAFE(q->now, cl->t_c, (u32)cl->mbuffer); printk(KERN_DEBUG "htb*c%x m=%d t=%ld c=%ld pq=%lu df=%ld ql=%d " "pa=%x f:", cl->classid,cl->cmode,cl->tokens,cl->ctokens, cl->pq_node.rb_color==-1?0:cl->pq_key,diff, cl->level?0:cl->un.leaf.q->q.qlen,cl->prio_activity); if (cl->level) for (p=0;p<TC_HTB_NUMPRIO;p++) { if (!cl->un.inner.feed[p].rb_node) continue; printk(" p%d a=%x:",p,cl->un.inner.ptr[p]?rb_entry(cl->un.inner.ptr[p], struct htb_class,node[p])->classid:0); HTB_DUMTREE(cl->un.inner.feed+p,node[p]); } printk("\n"); } }}#endif/** * 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 (HTB_ARGQ struct rb_root *root, struct htb_class *cl,int prio){ struct rb_node **p = &root->rb_node, *parent = NULL; HTB_DBG(7,3,"htb_add_id_tree cl=%X prio=%d\n",cl->classid,prio);#ifdef HTB_DEBUG if (cl->node[prio].rb_color != -1) { BUG_TRAP(0); return; } HTB_CHCL(cl); if (*p) { struct htb_class *x = rb_entry(*p,struct htb_class,node[prio]); HTB_CHCL(x); }#endif while (*p) { struct htb_class *c; parent = *p; c = rb_entry(parent, struct htb_class, node[prio]); HTB_CHCL(c); if (cl->classid > c->classid) p = &parent->rb_right; else p = &parent->rb_left; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -