/*-1* Copyright (c) 1991-1997 Regents of the University of California.2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. All advertising materials mentioning features or use of this software13* must display the following acknowledgement:14* This product includes software developed by the Network Research15* Group at Lawrence Berkeley Laboratory.16* 4. Neither the name of the University nor of the Laboratory may be used17* to endorse or promote products derived from this software without18* specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*32* $KAME: altq_rmclass.h,v 1.10 2003/08/20 23:30:23 itojun Exp $33*/3435#ifndef _ALTQ_ALTQ_RMCLASS_H_36#define _ALTQ_ALTQ_RMCLASS_H_3738#include <net/altq/altq_classq.h>3940#ifdef __cplusplus41extern "C" {42#endif4344#define RM_MAXPRIO 8 /* Max priority */4546#ifdef _KERNEL4748typedef struct mbuf mbuf_t;49typedef struct rm_ifdat rm_ifdat_t;50typedef struct rm_class rm_class_t;5152struct red;5354/*55* Macros for dealing with time values. We assume all times are56* 'timevals'. `microtime' is used to get the best available clock57* resolution. If `microtime' *doesn't* return a value that's about58* ten times smaller than the average packet time on the fastest59* link that will use these routines, a slightly different clock60* scheme than this one should be used.61* (Bias due to truncation error in this scheme will overestimate utilization62* and discriminate against high bandwidth classes. To remove this bias an63* integrator needs to be added. The simplest integrator uses a history of64* 10 * avg.packet.time / min.tick.time packet completion entries. This is65* straight forward to add but we don't want to pay the extra memory66* traffic to maintain it if it's not necessary (occasionally a vendor67* accidentally builds a workstation with a decent clock - e.g., Sun & HP).)68*/6970#define RM_GETTIME(now) microtime(&now)7172#define TV_LT(a, b) (((a)->tv_sec < (b)->tv_sec) || \73(((a)->tv_usec < (b)->tv_usec) && ((a)->tv_sec <= (b)->tv_sec)))7475#define TV_DELTA(a, b, delta) { \76int xxs; \77\78delta = (a)->tv_usec - (b)->tv_usec; \79if ((xxs = (a)->tv_sec - (b)->tv_sec)) { \80switch (xxs) { \81default: \82/* if (xxs < 0) \83printf("rm_class: bogus time values\n"); */ \84delta = 0; \85/* fall through */ \86case 2: \87delta += 1000000; \88/* fall through */ \89case 1: \90delta += 1000000; \91break; \92} \93} \94}9596#define TV_ADD_DELTA(a, delta, res) { \97int xxus = (a)->tv_usec + (delta); \98\99(res)->tv_sec = (a)->tv_sec; \100while (xxus >= 1000000) { \101++((res)->tv_sec); \102xxus -= 1000000; \103} \104(res)->tv_usec = xxus; \105}106107#define RM_TIMEOUT 2 /* 1 Clock tick. */108109#if 1110#define RM_MAXQUEUED 1 /* this isn't used in ALTQ/CBQ */111#else112#define RM_MAXQUEUED 16 /* Max number of packets downstream of CBQ */113#endif114#define RM_MAXQUEUE 64 /* Max queue length */115#define RM_FILTER_GAIN 5 /* log2 of gain, e.g., 5 => 31/32 */116#define RM_POWER (1 << RM_FILTER_GAIN)117#define RM_MAXDEPTH 32118#define RM_NS_PER_SEC (1000000000)119120typedef struct _rm_class_stats_ {121u_int handle;122u_int depth;123124struct pktcntr xmit_cnt; /* packets sent in this class */125struct pktcntr drop_cnt; /* dropped packets */126u_int over; /* # times went over limit */127u_int borrows; /* # times tried to borrow */128u_int overactions; /* # times invoked overlimit action */129u_int delays; /* # times invoked delay actions */130} rm_class_stats_t;131132/*133* CBQ Class state structure134*/135struct rm_class {136class_queue_t *q_; /* Queue of packets */137rm_ifdat_t *ifdat_;138int pri_; /* Class priority. */139int depth_; /* Class depth */140u_int ns_per_byte_; /* NanoSeconds per byte. */141u_int maxrate_; /* Bytes per second for this class. */142u_int allotment_; /* Fraction of link bandwidth. */143u_int w_allotment_; /* Weighted allotment for WRR */144int bytes_alloc_; /* Allocation for round of WRR */145146int avgidle_;147int maxidle_;148int minidle_;149int offtime_;150int sleeping_; /* != 0 if delaying */151int qthresh_; /* Queue threshold for formal link sharing */152int leaf_; /* Note whether leaf class or not.*/153154rm_class_t *children_; /* Children of this class */155rm_class_t *next_; /* Next pointer, used if child */156157rm_class_t *peer_; /* Peer class */158rm_class_t *borrow_; /* Borrow class */159rm_class_t *parent_; /* Parent class */160161void (*overlimit)(struct rm_class *, struct rm_class *);162void (*drop)(struct rm_class *); /* Class drop action. */163164union {165struct red *red_; /* RED state pointer */166struct codel *codel_; /* codel state pointer */167} cl_aqm_;168#define red_ cl_aqm_.red_169#define codel_ cl_aqm_.codel_170struct altq_pktattr *pktattr_; /* saved hdr used by RED/ECN */171int flags_;172173int last_pkttime_; /* saved pkt_time */174struct timeval undertime_; /* time can next send */175struct timeval last_; /* time last packet sent */176struct timeval overtime_;177struct callout callout_; /* for timeout() calls */178179rm_class_stats_t stats_; /* Class Statistics */180};181182/*183* CBQ Interface state184*/185struct rm_ifdat {186int queued_; /* # pkts queued downstream */187int efficient_; /* Link Efficiency bit */188int wrr_; /* Enable Weighted Round-Robin */189u_long ns_per_byte_; /* Link byte speed. */190int maxqueued_; /* Max packets to queue */191int maxpkt_; /* Max packet size. */192int qi_; /* In/out pointers for downstream */193int qo_; /* packets */194195/*196* Active class state and WRR state.197*/198rm_class_t *active_[RM_MAXPRIO]; /* Active cl's in each pri */199int na_[RM_MAXPRIO]; /* # of active cl's in a pri */200int num_[RM_MAXPRIO]; /* # of cl's per pri */201int alloc_[RM_MAXPRIO]; /* Byte Allocation */202u_long M_[RM_MAXPRIO]; /* WRR weights. */203204/*205* Network Interface/Solaris Queue state pointer.206*/207struct ifaltq *ifq_;208rm_class_t *default_; /* Default Pkt class, BE */209rm_class_t *root_; /* Root Link class. */210rm_class_t *ctl_; /* Control Traffic class. */211void (*restart)(struct ifaltq *); /* Restart routine. */212213/*214* Current packet downstream packet state and dynamic state.215*/216rm_class_t *borrowed_[RM_MAXQUEUED]; /* Class borrowed last */217rm_class_t *class_[RM_MAXQUEUED]; /* class sending */218int curlen_[RM_MAXQUEUED]; /* Current pktlen */219struct timeval now_[RM_MAXQUEUED]; /* Current packet time. */220int is_overlimit_[RM_MAXQUEUED];/* Current packet time. */221222int cutoff_; /* Cut-off depth for borrowing */223224struct timeval ifnow_; /* expected xmit completion time */225#if 1 /* ALTQ4PPP */226int maxiftime_; /* max delay inside interface */227#endif228rm_class_t *pollcache_; /* cached rm_class by poll operation */229};230231/* flags for rmc_init and rmc_newclass */232/* class flags; must be the same as class flags in altq_cbq.h */233#define RMCF_RED 0x0001234#define RMCF_ECN 0x0002235#define RMCF_RIO 0x0004236#define RMCF_FLOWVALVE 0x0008 /* use flowvalve (aka penalty-box) */237#define RMCF_CLEARDSCP 0x0010 /* clear diffserv codepoint */238#define RMCF_CODEL 0x0040239240/* flags for rmc_init */241#define RMCF_WRR 0x0100242#define RMCF_EFFICIENT 0x0200243244#define is_a_parent_class(cl) ((cl)->children_ != NULL)245246extern rm_class_t *rmc_newclass(int, struct rm_ifdat *, u_int,247void (*)(struct rm_class *, struct rm_class *),248int, struct rm_class *, struct rm_class *,249u_int, int, u_int, int, int);250extern void rmc_delete_class(struct rm_ifdat *, struct rm_class *);251extern int rmc_modclass(struct rm_class *, u_int, int,252u_int, int, u_int, int);253extern void rmc_init(struct ifaltq *, struct rm_ifdat *, u_int,254void (*)(struct ifaltq *),255int, int, u_int, int, u_int, int);256extern int rmc_queue_packet(struct rm_class *, mbuf_t *);257extern mbuf_t *rmc_dequeue_next(struct rm_ifdat *, int);258extern void rmc_update_class_util(struct rm_ifdat *);259extern void rmc_delay_action(struct rm_class *, struct rm_class *);260extern void rmc_dropall(struct rm_class *);261extern int rmc_get_weight(struct rm_ifdat *, int);262263#endif /* _KERNEL */264265#ifdef __cplusplus266}267#endif268269#endif /* _ALTQ_ALTQ_RMCLASS_H_ */270271272