Home | History | Annotate | Line # | Download | only in altq
if_altq.h revision 1.16.4.1
      1 /*	$NetBSD: if_altq.h,v 1.16.4.1 2023/11/11 13:16:30 thorpej Exp $	*/
      2 /*	$KAME: if_altq.h,v 1.12 2005/04/13 03:44:25 suz Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 1997-2003
      6  *	Sony Computer Science Laboratories Inc.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
     18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 #ifndef _ALTQ_IF_ALTQ_H_
     30 #define	_ALTQ_IF_ALTQ_H_
     31 
     32 #if defined(_KERNEL_OPT)
     33 #include "opt_altq_enabled.h"
     34 #endif
     35 
     36 struct altq_pktattr; struct tb_regulator; struct top_cdnr;
     37 
     38 /*
     39  * Structure defining a complex queue for a network interface.
     40  */
     41 struct ifaltq {
     42 	struct ifaltq *ifq_altq;	/* pointer back to self */
     43 	struct ifqueue *altq_ifq;	/* pointer back to base queue */
     44 	int	altq_type;		/* discipline type */
     45 	int	altq_flags;		/* flags (e.g. ready, in-use) */
     46 	void	*altq_disc;		/* for discipline-specific use */
     47 	struct	ifnet *altq_ifp;	/* back pointer to interface */
     48 
     49 	int	(*altq_enqueue)(struct ifaltq *, struct mbuf *);
     50 	struct	mbuf *(*altq_dequeue)(struct ifaltq *, int);
     51 	int	(*altq_request)(struct ifaltq *, int, void *);
     52 
     53 	/* classifier fields */
     54 	void	*altq_clfier;		/* classifier-specific use */
     55 	void	*(*altq_classify)(void *, struct mbuf *, int);
     56 
     57 	/* token bucket regulator */
     58 	struct	tb_regulator *altq_tbr;
     59 
     60 	/* input traffic conditioner (doesn't belong to the output queue...) */
     61 	struct top_cdnr *altq_cdnr;
     62 };
     63 
     64 
     65 #ifdef _KERNEL
     66 
     67 /*
     68  * packet attributes used by queueing disciplines.
     69  * pattr_class is a discipline-dependent scheduling class that is
     70  * set by a classifier.
     71  * pattr_hdr and pattr_af may be used by a discipline to access
     72  * the header within a mbuf.  (e.g. ECN needs to update the CE bit)
     73  * note that pattr_hdr could be stale after m_pullup, though link
     74  * layer output routines usually don't use m_pullup.  link-level
     75  * compression also invalidates these fields.  thus, pattr_hdr needs
     76  * to be verified when a discipline touches the header.
     77  */
     78 struct altq_pktattr {
     79 	void	*pattr_class;		/* sched class set by classifier */
     80 	int	pattr_af;		/* address family */
     81 	void *	pattr_hdr;		/* saved header position in mbuf */
     82 };
     83 
     84 /*
     85  * mbuf tag to carry a queue id (and hints for ECN).
     86  */
     87 struct altq_tag {
     88 	u_int32_t	qid;		/* queue id */
     89 	/* hints for ecn */
     90 	int		af;		/* address family */
     91 	void		*hdr;		/* saved header position in mbuf */
     92 };
     93 
     94 /*
     95  * a token-bucket regulator limits the rate that a network driver can
     96  * dequeue packets from the output queue.
     97  * modern cards are able to buffer a large amount of packets and dequeue
     98  * too many packets at a time.  this bursty dequeue behavior makes it
     99  * impossible to schedule packets by queueing disciplines.
    100  * a token-bucket is used to control the burst size in a device
    101  * independent manner.
    102  */
    103 struct tb_regulator {
    104 	int64_t		tbr_rate;	/* (scaled) token bucket rate */
    105 	int64_t		tbr_depth;	/* (scaled) token bucket depth */
    106 
    107 	int64_t		tbr_token;	/* (scaled) current token */
    108 	int64_t		tbr_filluptime;	/* (scaled) time to fill up bucket */
    109 	u_int64_t	tbr_last;	/* last time token was updated */
    110 
    111 	int		tbr_lastop;	/* last dequeue operation type
    112 					   needed for poll-and-dequeue */
    113 };
    114 
    115 /* if_altqflags */
    116 #define	ALTQF_READY	 0x01	/* driver supports alternate queueing */
    117 #define	ALTQF_ENABLED	 0x02	/* altq is in use */
    118 #define	ALTQF_CLASSIFY	 0x04	/* classify packets */
    119 #define	ALTQF_CNDTNING	 0x08	/* altq traffic conditioning is enabled */
    120 #define	ALTQF_DRIVER1	 0x40	/* driver specific */
    121 
    122 /* if_altqflags set internally only: */
    123 #define	ALTQF_CANTCHANGE 	(ALTQF_READY)
    124 
    125 /* altq_dequeue 2nd arg */
    126 #define	ALTDQ_REMOVE		1	/* dequeue mbuf from the queue */
    127 #define	ALTDQ_POLL		2	/* don't dequeue mbuf from the queue */
    128 
    129 /* altq request types (currently only purge is defined) */
    130 #define	ALTRQ_PURGE		1	/* purge all packets */
    131 
    132 #define	ALTQ_INC_LEN(altq)	(altq)->altq_ifq->ifq_len++
    133 #define	ALTQ_DEC_LEN(altq)	(altq)->altq_ifq->ifq_len--
    134 #define	ALTQ_GET_LEN(altq)	(altq)->altq_ifq->ifq_len
    135 #define	ALTQ_SET_LEN(altq, v)	(altq)->altq_ifq->ifq_len = (v)
    136 #define	ALTQ_IS_EMPTY(altq)	((altq)->altq_ifq->ifq_len == 0)
    137 
    138 #define	_ALTQ_FLAGS(ifq)	((ifq)->ifq_altq != NULL ?		\
    139 				 (ifq)->ifq_altq->altq_flags : 0)
    140 
    141 #define	ALTQ_IS_READY(ifq)		(_ALTQ_FLAGS(ifq) & ALTQF_READY)
    142 #define	ALTQ_IS_ENABLED(ifq)		(_ALTQ_FLAGS(ifq) & ALTQF_ENABLED)
    143 #define	ALTQ_NEEDS_CLASSIFY(ifq)	(_ALTQ_FLAGS(ifq) & ALTQF_CLASSIFY)
    144 #define	ALTQ_IS_CNDTNING(ifq)		(_ALTQ_FLAGS(ifq) & ALTQF_CNDTNING)
    145 
    146 #define	ALTQ_SET_CNDTNING(ifq)						\
    147 do {									\
    148 	if ((ifq)->ifq_altq != NULL) {					\
    149 		(ifq)->ifq_altq->altq_flags |= ALTQF_CNDTNING;		\
    150 	}								\
    151 } while (/*CONSTCOND*/0)
    152 
    153 #define	ALTQ_CLEAR_CNDTNING(ifq)					\
    154 do {									\
    155 	if ((ifq)->ifq_altq != NULL) {					\
    156 		(ifq)->ifq_altq->altq_flags &= ~ALTQF_CNDTNING;		\
    157 	}								\
    158 } while (/*CONSTCOND*/0)
    159 
    160 #define	ALTQ_IS_ATTACHED(ifq)		((ifq)->ifq_altq != NULL &&	\
    161 					 (ifq)->ifq_altq->altq_disc != NULL)
    162 
    163 #define	ALTQ_ENQUEUE(ifq, m, err)					\
    164 	(err) = (*(ifq)->ifq_altq->altq_enqueue)((ifq)->ifq_altq,(m))
    165 #define	ALTQ_DEQUEUE(ifq, m)						\
    166 	(m) = (*(ifq)->ifq_altq->altq_dequeue)((ifq)->ifq_altq, ALTDQ_REMOVE)
    167 #define	ALTQ_POLL(ifq, m)						\
    168 	(m) = (*(ifq)->ifq_altq->altq_dequeue)((ifq)->ifq_altq, ALTDQ_POLL)
    169 #define	ALTQ_PURGE(ifq)							\
    170 	(void)(*(ifq)->ifq_altq->altq_request)((ifq)->ifq_altq, ALTRQ_PURGE, (void *)0)
    171 
    172 #define	TBR_IS_ENABLED(ifq)		((ifq)->ifq_altq != NULL &&	\
    173 					 (ifq)->ifq_altq->altq_tbr != NULL)
    174 
    175 extern void altq_alloc(struct ifqueue *, struct ifnet *);
    176 extern void altq_free(struct ifqueue *);
    177 extern void altq_set_ready(struct ifqueue *);
    178 
    179 extern int altq_attach(struct ifaltq *, int, void *,
    180 		       int (*)(struct ifaltq *, struct mbuf *),
    181 		       struct mbuf *(*)(struct ifaltq *, int),
    182 		       int (*)(struct ifaltq *, int, void *),
    183 		       void *,
    184 		       void *(*)(void *, struct mbuf *, int));
    185 extern int altq_detach(struct ifaltq *);
    186 extern int altq_enable(struct ifaltq *);
    187 extern int altq_disable(struct ifaltq *);
    188 extern struct mbuf *tbr_dequeue(struct ifaltq *, int);
    189 extern int (*altq_input)(struct mbuf *, int);
    190 #if 1 /* ALTQ3_CLFIER_COMPAT */
    191 void altq_etherclassify(struct ifaltq *, struct mbuf *);
    192 #endif
    193 #endif /* _KERNEL */
    194 
    195 #endif /* _ALTQ_IF_ALTQ_H_ */
    196