1130384Smlaier/*	$FreeBSD$	*/
2219457Sjkim/*	$KAME: if_altq.h,v 1.12 2005/04/13 03:44:25 suz Exp $	*/
3130365Smlaier
4130365Smlaier/*
5130365Smlaier * Copyright (C) 1997-2003
6130365Smlaier *	Sony Computer Science Laboratories Inc.  All rights reserved.
7130365Smlaier *
8130365Smlaier * Redistribution and use in source and binary forms, with or without
9130365Smlaier * modification, are permitted provided that the following conditions
10130365Smlaier * are met:
11130365Smlaier * 1. Redistributions of source code must retain the above copyright
12130365Smlaier *    notice, this list of conditions and the following disclaimer.
13130365Smlaier * 2. Redistributions in binary form must reproduce the above copyright
14130365Smlaier *    notice, this list of conditions and the following disclaimer in the
15130365Smlaier *    documentation and/or other materials provided with the distribution.
16130365Smlaier *
17130365Smlaier * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
18130365Smlaier * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19130365Smlaier * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20130365Smlaier * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
21130365Smlaier * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22130365Smlaier * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23130365Smlaier * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24130365Smlaier * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25130365Smlaier * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26130365Smlaier * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27130365Smlaier * SUCH DAMAGE.
28130365Smlaier */
29130365Smlaier#ifndef _ALTQ_IF_ALTQ_H_
30130365Smlaier#define	_ALTQ_IF_ALTQ_H_
31130365Smlaier
32219457Sjkim#ifdef __FreeBSD__
33130365Smlaier#include <sys/lock.h>		/* XXX */
34130365Smlaier#include <sys/mutex.h>		/* XXX */
35130365Smlaier#include <sys/event.h>		/* XXX */
36130365Smlaier#endif
37130365Smlaier
38130365Smlaier#ifdef _KERNEL_OPT
39130365Smlaier#include <altq/altqconf.h>
40130365Smlaier#endif
41130365Smlaier
42130365Smlaierstruct altq_pktattr; struct tb_regulator; struct top_cdnr;
43130365Smlaier
44130365Smlaier/*
45130365Smlaier * Structure defining a queue for a network interface.
46130365Smlaier */
47130365Smlaierstruct	ifaltq {
48130365Smlaier	/* fields compatible with struct ifqueue */
49130365Smlaier	struct	mbuf *ifq_head;
50130365Smlaier	struct	mbuf *ifq_tail;
51130365Smlaier	int	ifq_len;
52130365Smlaier	int	ifq_maxlen;
53130365Smlaier	int	ifq_drops;
54219457Sjkim#ifdef __FreeBSD__
55130365Smlaier	struct	mtx ifq_mtx;
56130365Smlaier#endif
57130365Smlaier
58130384Smlaier	/* driver owned queue (used for bulk dequeue and prepend) UNLOCKED */
59130384Smlaier	struct	mbuf *ifq_drv_head;
60130384Smlaier	struct	mbuf *ifq_drv_tail;
61130384Smlaier	int	ifq_drv_len;
62130384Smlaier	int	ifq_drv_maxlen;
63130384Smlaier
64130365Smlaier	/* alternate queueing related fields */
65130365Smlaier	int	altq_type;		/* discipline type */
66130365Smlaier	int	altq_flags;		/* flags (e.g. ready, in-use) */
67130365Smlaier	void	*altq_disc;		/* for discipline-specific use */
68130365Smlaier	struct	ifnet *altq_ifp;	/* back pointer to interface */
69130365Smlaier
70130365Smlaier	int	(*altq_enqueue)(struct ifaltq *, struct mbuf *,
71130365Smlaier				struct altq_pktattr *);
72130365Smlaier	struct	mbuf *(*altq_dequeue)(struct ifaltq *, int);
73130365Smlaier	int	(*altq_request)(struct ifaltq *, int, void *);
74130365Smlaier
75130365Smlaier	/* classifier fields */
76130365Smlaier	void	*altq_clfier;		/* classifier-specific use */
77130365Smlaier	void	*(*altq_classify)(void *, struct mbuf *, int);
78130365Smlaier
79130365Smlaier	/* token bucket regulator */
80130365Smlaier	struct	tb_regulator *altq_tbr;
81130365Smlaier
82130365Smlaier	/* input traffic conditioner (doesn't belong to the output queue...) */
83130365Smlaier	struct top_cdnr *altq_cdnr;
84130365Smlaier};
85130365Smlaier
86130365Smlaier
87130365Smlaier#ifdef _KERNEL
88130365Smlaier
89130365Smlaier/*
90130365Smlaier * packet attributes used by queueing disciplines.
91130365Smlaier * pattr_class is a discipline-dependent scheduling class that is
92130365Smlaier * set by a classifier.
93130365Smlaier * pattr_hdr and pattr_af may be used by a discipline to access
94130365Smlaier * the header within a mbuf.  (e.g. ECN needs to update the CE bit)
95130365Smlaier * note that pattr_hdr could be stale after m_pullup, though link
96130365Smlaier * layer output routines usually don't use m_pullup.  link-level
97130365Smlaier * compression also invalidates these fields.  thus, pattr_hdr needs
98130365Smlaier * to be verified when a discipline touches the header.
99130365Smlaier */
100130365Smlaierstruct altq_pktattr {
101130365Smlaier	void	*pattr_class;		/* sched class set by classifier */
102130365Smlaier	int	pattr_af;		/* address family */
103130365Smlaier	caddr_t	pattr_hdr;		/* saved header position in mbuf */
104130365Smlaier};
105130365Smlaier
106130365Smlaier/*
107130365Smlaier * mbuf tag to carry a queue id (and hints for ECN).
108130365Smlaier */
109130365Smlaierstruct altq_tag {
110130365Smlaier	u_int32_t	qid;		/* queue id */
111130365Smlaier	/* hints for ecn */
112130365Smlaier	int		af;		/* address family */
113130365Smlaier	void		*hdr;		/* saved header position in mbuf */
114130365Smlaier};
115130365Smlaier
116130365Smlaier/*
117130365Smlaier * a token-bucket regulator limits the rate that a network driver can
118130365Smlaier * dequeue packets from the output queue.
119130365Smlaier * modern cards are able to buffer a large amount of packets and dequeue
120130365Smlaier * too many packets at a time.  this bursty dequeue behavior makes it
121130365Smlaier * impossible to schedule packets by queueing disciplines.
122130365Smlaier * a token-bucket is used to control the burst size in a device
123130365Smlaier * independent manner.
124130365Smlaier */
125130365Smlaierstruct tb_regulator {
126130365Smlaier	int64_t		tbr_rate;	/* (scaled) token bucket rate */
127130365Smlaier	int64_t		tbr_depth;	/* (scaled) token bucket depth */
128130365Smlaier
129130365Smlaier	int64_t		tbr_token;	/* (scaled) current token */
130130365Smlaier	int64_t		tbr_filluptime;	/* (scaled) time to fill up bucket */
131130365Smlaier	u_int64_t	tbr_last;	/* last time token was updated */
132130365Smlaier
133130365Smlaier	int		tbr_lastop;	/* last dequeue operation type
134130365Smlaier					   needed for poll-and-dequeue */
135130365Smlaier};
136130365Smlaier
137130365Smlaier/* if_altqflags */
138130365Smlaier#define	ALTQF_READY	 0x01	/* driver supports alternate queueing */
139130365Smlaier#define	ALTQF_ENABLED	 0x02	/* altq is in use */
140130365Smlaier#define	ALTQF_CLASSIFY	 0x04	/* classify packets */
141130365Smlaier#define	ALTQF_CNDTNING	 0x08	/* altq traffic conditioning is enabled */
142130365Smlaier#define	ALTQF_DRIVER1	 0x40	/* driver specific */
143130365Smlaier
144130365Smlaier/* if_altqflags set internally only: */
145130365Smlaier#define	ALTQF_CANTCHANGE 	(ALTQF_READY)
146130365Smlaier
147130365Smlaier/* altq_dequeue 2nd arg */
148130365Smlaier#define	ALTDQ_REMOVE		1	/* dequeue mbuf from the queue */
149130365Smlaier#define	ALTDQ_POLL		2	/* don't dequeue mbuf from the queue */
150130365Smlaier
151130365Smlaier/* altq request types (currently only purge is defined) */
152130365Smlaier#define	ALTRQ_PURGE		1	/* purge all packets */
153130365Smlaier
154130365Smlaier#define	ALTQ_IS_READY(ifq)		((ifq)->altq_flags & ALTQF_READY)
155130365Smlaier#define	ALTQ_IS_ENABLED(ifq)		((ifq)->altq_flags & ALTQF_ENABLED)
156130365Smlaier#define	ALTQ_NEEDS_CLASSIFY(ifq)	((ifq)->altq_flags & ALTQF_CLASSIFY)
157130365Smlaier#define	ALTQ_IS_CNDTNING(ifq)		((ifq)->altq_flags & ALTQF_CNDTNING)
158130365Smlaier
159130365Smlaier#define	ALTQ_SET_CNDTNING(ifq)		((ifq)->altq_flags |= ALTQF_CNDTNING)
160130365Smlaier#define	ALTQ_CLEAR_CNDTNING(ifq)	((ifq)->altq_flags &= ~ALTQF_CNDTNING)
161130365Smlaier#define	ALTQ_IS_ATTACHED(ifq)		((ifq)->altq_disc != NULL)
162130365Smlaier
163130365Smlaier#define	ALTQ_ENQUEUE(ifq, m, pa, err)					\
164130365Smlaier	(err) = (*(ifq)->altq_enqueue)((ifq),(m),(pa))
165130365Smlaier#define	ALTQ_DEQUEUE(ifq, m)						\
166130365Smlaier	(m) = (*(ifq)->altq_dequeue)((ifq), ALTDQ_REMOVE)
167130365Smlaier#define	ALTQ_POLL(ifq, m)						\
168130365Smlaier	(m) = (*(ifq)->altq_dequeue)((ifq), ALTDQ_POLL)
169130365Smlaier#define	ALTQ_PURGE(ifq)							\
170130365Smlaier	(void)(*(ifq)->altq_request)((ifq), ALTRQ_PURGE, (void *)0)
171130365Smlaier#define	ALTQ_IS_EMPTY(ifq)		((ifq)->ifq_len == 0)
172130365Smlaier#define	TBR_IS_ENABLED(ifq)		((ifq)->altq_tbr != NULL)
173130365Smlaier
174130365Smlaierextern int altq_attach(struct ifaltq *, int, void *,
175130365Smlaier		       int (*)(struct ifaltq *, struct mbuf *,
176130365Smlaier			       struct altq_pktattr *),
177130365Smlaier		       struct mbuf *(*)(struct ifaltq *, int),
178130365Smlaier		       int (*)(struct ifaltq *, int, void *),
179130365Smlaier		       void *,
180130365Smlaier		       void *(*)(void *, struct mbuf *, int));
181130365Smlaierextern int altq_detach(struct ifaltq *);
182130365Smlaierextern int altq_enable(struct ifaltq *);
183130365Smlaierextern int altq_disable(struct ifaltq *);
184130508Smlaierextern struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int);
185130365Smlaierextern int (*altq_input)(struct mbuf *, int);
186130508Smlaier#if 0 /* ALTQ3_CLFIER_COMPAT */
187130365Smlaiervoid altq_etherclassify(struct ifaltq *, struct mbuf *, struct altq_pktattr *);
188130365Smlaier#endif
189130365Smlaier#endif /* _KERNEL */
190130365Smlaier
191130365Smlaier#endif /* _ALTQ_IF_ALTQ_H_ */
192