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