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