Home | History | Annotate | Line # | Download | only in altq
      1  1.14    ozaki /*	$NetBSD: altq_rmclass.h,v 1.14 2025/02/03 07:40:24 ozaki-r Exp $	*/
      2   1.7    peter /*	$KAME: altq_rmclass.h,v 1.10 2003/08/20 23:30:23 itojun Exp $	*/
      3   1.1  thorpej 
      4   1.1  thorpej /*
      5   1.1  thorpej  * Copyright (c) 1991-1997 Regents of the University of California.
      6   1.1  thorpej  * All rights reserved.
      7   1.1  thorpej  *
      8   1.1  thorpej  * Redistribution and use in source and binary forms, with or without
      9   1.1  thorpej  * modification, are permitted provided that the following conditions
     10   1.1  thorpej  * are met:
     11   1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     12   1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     13   1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     15   1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     16   1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     17   1.1  thorpej  *    must display the following acknowledgement:
     18   1.1  thorpej  *	This product includes software developed by the Network Research
     19   1.1  thorpej  *	Group at Lawrence Berkeley Laboratory.
     20   1.1  thorpej  * 4. Neither the name of the University nor of the Laboratory may be used
     21   1.1  thorpej  *    to endorse or promote products derived from this software without
     22   1.1  thorpej  *    specific prior written permission.
     23   1.1  thorpej  *
     24   1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25   1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26   1.1  thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27   1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28   1.1  thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29   1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30   1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31   1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32   1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33   1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34   1.1  thorpej  * SUCH DAMAGE.
     35   1.1  thorpej  */
     36   1.1  thorpej 
     37   1.1  thorpej #ifndef _ALTQ_ALTQ_RMCLASS_H_
     38   1.1  thorpej #define	_ALTQ_ALTQ_RMCLASS_H_
     39   1.1  thorpej 
     40   1.1  thorpej #include <altq/altq_classq.h>
     41   1.1  thorpej 
     42   1.1  thorpej /* #pragma ident "@(#)rm_class.h  1.20     97/10/23 SMI" */
     43   1.1  thorpej 
     44   1.1  thorpej #ifdef __cplusplus
     45   1.1  thorpej extern "C" {
     46   1.1  thorpej #endif
     47   1.1  thorpej 
     48   1.4   itojun #define	RM_MAXPRIO	8	/* Max priority */
     49   1.4   itojun 
     50   1.1  thorpej #ifdef _KERNEL
     51   1.1  thorpej 
     52   1.1  thorpej typedef struct mbuf		mbuf_t;
     53   1.1  thorpej typedef struct rm_ifdat		rm_ifdat_t;
     54   1.1  thorpej typedef struct rm_class		rm_class_t;
     55   1.1  thorpej 
     56   1.1  thorpej struct red;
     57   1.1  thorpej 
     58  1.11    ozaki #define	RM_GETTIME(now) nanotime(&now)
     59   1.1  thorpej 
     60  1.11    ozaki #define	TS_LT(a, b) (((a)->tv_sec < (b)->tv_sec) ||  \
     61  1.11    ozaki 	(((a)->tv_nsec < (b)->tv_nsec) && ((a)->tv_sec <= (b)->tv_sec)))
     62   1.1  thorpej 
     63  1.11    ozaki #define	TS_DELTA(a, b, delta) do { \
     64  1.11    ozaki 	register int64_t	xxs;	\
     65   1.1  thorpej 							\
     66  1.11    ozaki 	delta = (int64_t)((a)->tv_nsec - (b)->tv_nsec); \
     67   1.1  thorpej 	if ((xxs = (a)->tv_sec - (b)->tv_sec)) { \
     68   1.1  thorpej 		switch (xxs) { \
     69   1.1  thorpej 		default: \
     70   1.3   itojun 			/* if (xxs < 0) \
     71   1.3   itojun 				printf("rm_class: bogus time values\n"); */ \
     72   1.1  thorpej 			delta = 0; \
     73   1.1  thorpej 			/* fall through */ \
     74   1.1  thorpej 		case 2: \
     75  1.11    ozaki 			delta += 1000000000; \
     76   1.1  thorpej 			/* fall through */ \
     77   1.1  thorpej 		case 1: \
     78  1.11    ozaki 			delta += 1000000000; \
     79   1.1  thorpej 			break; \
     80   1.1  thorpej 		} \
     81   1.1  thorpej 	} \
     82   1.9    ozaki } while (0)
     83   1.1  thorpej 
     84  1.11    ozaki #define	TS_ADD_DELTA(a, delta, res) do { \
     85  1.14    ozaki 	KASSERT(delta >= 0); \
     86  1.14    ozaki 	(res)->tv_sec = (a)->tv_sec + (delta) / 1000000000L; \
     87  1.14    ozaki 	(res)->tv_nsec = (a)->tv_nsec + (long)((delta) % 1000000000L); \
     88  1.14    ozaki 	if ((res)->tv_nsec >= 1000000000L) { \
     89  1.14    ozaki 		(res)->tv_nsec -= 1000000000L; \
     90  1.14    ozaki 		(res)->tv_sec++; \
     91   1.1  thorpej 	} \
     92  1.14    ozaki 	KASSERT((res)->tv_nsec >= 0); \
     93   1.9    ozaki } while (0)
     94   1.1  thorpej 
     95   1.1  thorpej #define	RM_TIMEOUT	2	/* 1 Clock tick. */
     96   1.1  thorpej 
     97   1.1  thorpej #if 1
     98   1.1  thorpej #define	RM_MAXQUEUED	1	/* this isn't used in ALTQ/CBQ */
     99   1.1  thorpej #else
    100   1.1  thorpej #define	RM_MAXQUEUED	16	/* Max number of packets downstream of CBQ */
    101   1.1  thorpej #endif
    102   1.1  thorpej #define	RM_MAXQUEUE	64	/* Max queue length */
    103   1.1  thorpej #define	RM_FILTER_GAIN	5	/* log2 of gain, e.g., 5 => 31/32 */
    104   1.1  thorpej #define	RM_POWER	(1 << RM_FILTER_GAIN)
    105   1.1  thorpej #define	RM_MAXDEPTH	32
    106   1.1  thorpej #define	RM_NS_PER_SEC	(1000000000)
    107  1.10    ozaki #define	RM_PS_PER_SEC	(1000000000000)
    108   1.1  thorpej 
    109   1.1  thorpej typedef struct _rm_class_stats_ {
    110   1.1  thorpej 	u_int		handle;
    111   1.1  thorpej 	u_int		depth;
    112   1.1  thorpej 
    113   1.1  thorpej 	struct pktcntr	xmit_cnt;	/* packets sent in this class */
    114   1.1  thorpej 	struct pktcntr	drop_cnt;	/* dropped packets */
    115   1.1  thorpej 	u_int		over;		/* # times went over limit */
    116   1.1  thorpej 	u_int		borrows;	/* # times tried to borrow */
    117   1.1  thorpej 	u_int		overactions;	/* # times invoked overlimit action */
    118   1.1  thorpej 	u_int		delays;		/* # times invoked delay actions */
    119   1.1  thorpej } rm_class_stats_t;
    120   1.1  thorpej 
    121   1.1  thorpej /*
    122   1.1  thorpej  * CBQ Class state structure
    123   1.1  thorpej  */
    124   1.1  thorpej struct rm_class {
    125   1.1  thorpej 	class_queue_t	*q_;		/* Queue of packets */
    126   1.5    perry 	rm_ifdat_t	*ifdat_;
    127   1.1  thorpej 	int		pri_;		/* Class priority. */
    128   1.1  thorpej 	int		depth_;		/* Class depth */
    129  1.12    ozaki 	uint64_t	ps_per_byte_;	/* PicoSeconds per byte. */
    130   1.1  thorpej 	u_int		maxrate_;	/* Bytes per second for this class. */
    131   1.1  thorpej 	u_int		allotment_;	/* Fraction of link bandwidth. */
    132   1.1  thorpej 	u_int		w_allotment_;	/* Weighted allotment for WRR */
    133   1.1  thorpej 	int		bytes_alloc_;	/* Allocation for round of WRR */
    134   1.1  thorpej 
    135  1.12    ozaki 	int64_t		avgidle_;
    136  1.12    ozaki 	int64_t		maxidle_;
    137  1.12    ozaki 	int64_t		minidle_;
    138  1.12    ozaki 	int64_t		offtime_;
    139   1.1  thorpej 	int		sleeping_;	/* != 0 if delaying */
    140   1.1  thorpej 	int		qthresh_;	/* Queue threshold for formal link sharing */
    141   1.1  thorpej 	int		leaf_;		/* Note whether leaf class or not.*/
    142   1.1  thorpej 
    143   1.1  thorpej 	rm_class_t	*children_;	/* Children of this class */
    144   1.1  thorpej 	rm_class_t	*next_;		/* Next pointer, used if child */
    145   1.1  thorpej 
    146   1.1  thorpej 	rm_class_t	*peer_;		/* Peer class */
    147   1.1  thorpej 	rm_class_t	*borrow_;	/* Borrow class */
    148   1.1  thorpej 	rm_class_t	*parent_;	/* Parent class */
    149   1.1  thorpej 
    150   1.1  thorpej 	void	(*overlimit)(struct rm_class *, struct rm_class *);
    151   1.1  thorpej 	void	(*drop)(struct rm_class *);       /* Class drop action. */
    152   1.1  thorpej 
    153   1.1  thorpej 	struct red	*red_;		/* RED state pointer */
    154   1.1  thorpej 	struct altq_pktattr *pktattr_;	/* saved hdr used by RED/ECN */
    155   1.1  thorpej 	int		flags_;
    156   1.1  thorpej 
    157  1.12    ozaki 	int64_t		last_pkttime_;	/* saved pkt_time */
    158  1.11    ozaki 	struct timespec	undertime_;	/* time can next send */
    159  1.11    ozaki 	struct timespec	last_;		/* time last packet sent */
    160  1.11    ozaki 	struct timespec	overtime_;
    161   1.1  thorpej 	struct callout	callout_; 	/* for timeout() calls */
    162   1.1  thorpej 
    163   1.1  thorpej 	rm_class_stats_t stats_;	/* Class Statistics */
    164   1.1  thorpej };
    165   1.1  thorpej 
    166   1.1  thorpej /*
    167   1.1  thorpej  * CBQ Interface state
    168   1.1  thorpej  */
    169   1.1  thorpej struct rm_ifdat {
    170   1.1  thorpej 	int		queued_;	/* # pkts queued downstream */
    171  1.13   andvar 	int		efficient_;	/* Link Efficiency bit */
    172   1.1  thorpej 	int		wrr_;		/* Enable Weighted Round-Robin */
    173  1.12    ozaki 	uint64_t	ps_per_byte_;	/* Link byte speed. */
    174   1.1  thorpej 	int		maxqueued_;	/* Max packets to queue */
    175   1.1  thorpej 	int		maxpkt_;	/* Max packet size. */
    176   1.1  thorpej 	int		qi_;		/* In/out pointers for downstream */
    177   1.1  thorpej 	int		qo_;		/* packets */
    178   1.1  thorpej 
    179   1.1  thorpej 	/*
    180   1.1  thorpej 	 * Active class state and WRR state.
    181   1.1  thorpej 	 */
    182   1.1  thorpej 	rm_class_t	*active_[RM_MAXPRIO];	/* Active cl's in each pri */
    183   1.1  thorpej 	int		na_[RM_MAXPRIO];	/* # of active cl's in a pri */
    184   1.1  thorpej 	int		num_[RM_MAXPRIO];	/* # of cl's per pri */
    185   1.1  thorpej 	int		alloc_[RM_MAXPRIO];	/* Byte Allocation */
    186   1.1  thorpej 	u_long		M_[RM_MAXPRIO];		/* WRR weights. */
    187   1.1  thorpej 
    188   1.1  thorpej 	/*
    189   1.1  thorpej 	 * Network Interface/Solaris Queue state pointer.
    190   1.1  thorpej 	 */
    191   1.1  thorpej 	struct ifaltq	*ifq_;
    192   1.1  thorpej 	rm_class_t	*default_;	/* Default Pkt class, BE */
    193   1.1  thorpej 	rm_class_t	*root_;		/* Root Link class. */
    194   1.1  thorpej 	rm_class_t	*ctl_;		/* Control Traffic class. */
    195   1.1  thorpej 	void		(*restart)(struct ifaltq *);	/* Restart routine. */
    196   1.1  thorpej 
    197   1.1  thorpej 	/*
    198   1.1  thorpej 	 * Current packet downstream packet state and dynamic state.
    199   1.1  thorpej 	 */
    200   1.1  thorpej 	rm_class_t	*borrowed_[RM_MAXQUEUED]; /* Class borrowed last */
    201   1.1  thorpej 	rm_class_t	*class_[RM_MAXQUEUED];	/* class sending */
    202   1.1  thorpej 	int		curlen_[RM_MAXQUEUED];	/* Current pktlen */
    203  1.11    ozaki 	struct timespec	now_[RM_MAXQUEUED];	/* Current packet time. */
    204   1.1  thorpej 	int		is_overlimit_[RM_MAXQUEUED];/* Current packet time. */
    205   1.1  thorpej 
    206   1.1  thorpej 	int		cutoff_;	/* Cut-off depth for borrowing */
    207   1.1  thorpej 
    208  1.11    ozaki 	struct timespec	ifnow_;		/* expected xmit completion time */
    209   1.1  thorpej #if 1 /* ALTQ4PPP */
    210   1.1  thorpej 	int		maxiftime_;	/* max delay inside interface */
    211   1.1  thorpej #endif
    212   1.1  thorpej         rm_class_t	*pollcache_;	/* cached rm_class by poll operation */
    213   1.1  thorpej };
    214   1.1  thorpej 
    215   1.1  thorpej /* flags for rmc_init and rmc_newclass */
    216   1.1  thorpej /* class flags */
    217   1.1  thorpej #define	RMCF_RED		0x0001
    218   1.1  thorpej #define	RMCF_ECN		0x0002
    219   1.1  thorpej #define	RMCF_RIO		0x0004
    220   1.1  thorpej #define	RMCF_FLOWVALVE		0x0008	/* use flowvalve (aka penalty-box) */
    221   1.1  thorpej #define	RMCF_CLEARDSCP		0x0010  /* clear diffserv codepoint */
    222   1.1  thorpej 
    223   1.1  thorpej /* flags for rmc_init */
    224   1.1  thorpej #define	RMCF_WRR		0x0100
    225   1.1  thorpej #define	RMCF_EFFICIENT		0x0200
    226   1.1  thorpej 
    227   1.1  thorpej #define	is_a_parent_class(cl)	((cl)->children_ != NULL)
    228   1.1  thorpej 
    229  1.12    ozaki extern rm_class_t *rmc_newclass(int, struct rm_ifdat *, uint64_t,
    230   1.7    peter 				void (*)(struct rm_class *, struct rm_class *),
    231   1.7    peter 				int, struct rm_class *, struct rm_class *,
    232   1.7    peter 				u_int, int, u_int, int, int);
    233   1.7    peter extern void	rmc_delete_class(struct rm_ifdat *, struct rm_class *);
    234  1.12    ozaki extern int 	rmc_modclass(struct rm_class *, uint64_t, int,
    235   1.7    peter 			     u_int, int, u_int, int);
    236  1.12    ozaki extern int	rmc_init(struct ifaltq *, struct rm_ifdat *, uint64_t,
    237   1.7    peter 			 void (*)(struct ifaltq *),
    238   1.7    peter 			 int, int, u_int, int, u_int, int);
    239   1.7    peter extern int	rmc_queue_packet(struct rm_class *, mbuf_t *);
    240   1.7    peter extern mbuf_t	*rmc_dequeue_next(struct rm_ifdat *, int);
    241   1.7    peter extern void	rmc_update_class_util(struct rm_ifdat *);
    242   1.7    peter extern void	rmc_delay_action(struct rm_class *, struct rm_class *);
    243   1.7    peter extern void	rmc_dropall(struct rm_class *);
    244   1.7    peter extern int	rmc_get_weight(struct rm_ifdat *, int);
    245   1.1  thorpej 
    246   1.1  thorpej #endif /* _KERNEL */
    247   1.1  thorpej 
    248   1.1  thorpej #ifdef __cplusplus
    249   1.1  thorpej }
    250   1.1  thorpej #endif
    251   1.1  thorpej 
    252   1.1  thorpej #endif /* _ALTQ_ALTQ_RMCLASS_H_ */
    253