if: Multiple TX queue support step 1 of many; introduce ifaltq subqueue
[dragonfly.git] / sys / net / altq / if_altq.h
1 /*      $KAME: if_altq.h,v 1.11 2003/07/10 12:07:50 kjc Exp $   */
2
3 /*
4  * Copyright (C) 1997-2003
5  *      Sony Computer Science Laboratories Inc.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 #ifndef _NET_ALTQ_IF_ALTQ_H_
29 #define _NET_ALTQ_IF_ALTQ_H_
30
31 #ifndef _SYS_SERIALIZE_H_
32 #include <sys/serialize.h>
33 #endif
34
35 /* Default subqueue */
36 #define ALTQ_SUBQ_INDEX_DEFAULT 0
37
38 struct mbuf;
39 struct altq_pktattr;
40
41 struct ifaltq_subque;
42 struct ifaltq;
43
44 typedef int (*ifsq_enqueue_t)(struct ifaltq_subque *, struct mbuf *,
45     struct altq_pktattr *);
46 typedef struct mbuf *(*ifsq_dequeue_t)(struct ifaltq_subque *,
47     struct mbuf *, int);
48 typedef int (*ifsq_request_t)(struct ifaltq_subque *, int, void *);
49
50 struct ifsubq_stage {
51         struct ifaltq_subque *stg_subq;
52         int             stg_cnt;
53         int             stg_len;
54         uint32_t        stg_flags;
55         TAILQ_ENTRY(ifsubq_stage) stg_link;
56 } __cachealign;
57
58 #define IFSQ_STAGE_FLAG_QUED    0x1
59 #define IFSQ_STAGE_FLAG_SCHED   0x2
60
61 struct ifaltq_subque {
62         struct lwkt_serialize ifsq_lock;
63         int             ifsq_index;
64
65         struct ifaltq   *ifsq_altq;
66         struct ifnet    *ifsq_ifp;
67         void            *ifsq_hw_priv;  /* hw private data */
68
69         /* fields compatible with IFQ_ macros */
70         struct mbuf     *ifq_head;
71         struct mbuf     *ifq_tail;
72         int             ifq_len;
73         int             ifq_maxlen;
74
75         ifsq_enqueue_t  ifsq_enqueue;
76         ifsq_dequeue_t  ifsq_dequeue;
77         ifsq_request_t  ifsq_request;
78
79         struct mbuf     *ifsq_prepended;/* mbuf dequeued, but not yet xmit */
80         int             ifsq_started;   /* ifnet.if_start interlock */
81         int             ifsq_hw_oactive;/* hw too busy, protected by driver */
82         int             ifsq_cpuid;     /* owner cpu */
83         struct ifsubq_stage *ifsq_stage;/* packet staging information */
84         struct netmsg_base *ifsq_ifstart_nmsg;
85                                         /* percpu msgs to sched if_start */
86 } __cachealign;
87
88 #ifdef _KERNEL
89 #define ALTQ_SQ_ASSERT_LOCKED(ifsq)     ASSERT_SERIALIZED(&(ifsq)->ifsq_lock)
90 #define ALTQ_SQ_LOCK_INIT(ifsq)         lwkt_serialize_init(&(ifsq)->ifsq_lock)
91 #define ALTQ_SQ_LOCK(ifsq) \
92         lwkt_serialize_adaptive_enter(&(ifsq)->ifsq_lock)
93 #define ALTQ_SQ_UNLOCK(ifsq)            lwkt_serialize_exit(&(ifsq)->ifsq_lock)
94 #endif
95
96 /*
97  * Structure defining a queue for a network interface.
98  */
99 struct  ifaltq {
100         /* alternate queueing related fields */
101         int     altq_type;              /* discipline type */
102         int     altq_flags;             /* flags (e.g. ready, in-use) */
103         void    *altq_disc;             /* for discipline-specific use */
104         struct  ifnet *altq_ifp;        /* back pointer to interface */
105
106         /* classifier fields */
107         void    *altq_clfier;           /* classifier-specific use */
108         void    *(*altq_classify)(struct ifaltq *, struct mbuf *,
109                                   struct altq_pktattr *);
110         void    *altq_unused;
111
112         /* token bucket regulator */
113         struct  tb_regulator *altq_tbr;
114
115         /* Sub-queues */
116         int     altq_subq_cnt;
117         struct ifaltq_subque *altq_subq;
118
119         int     altq_maxlen;
120 };
121
122 #ifdef _KERNRL
123 /* COMPAT */
124 #define ALTQ_LOCK(ifq) \
125         ALTQ_SQ_LOCK(&(ifq)->altq_subq[ALTQ_SUBQ_INDEX_DEFAULT])
126 /* COMPAT */
127 #define ALTQ_UNLOCK(ifq) \
128         ALTQ_SQ_UNLOCK(&(ifq)->altq_subq[ALTQ_SUBQ_INDEX_DEFAULT])
129 #endif
130
131 #ifdef _KERNEL
132
133 /*
134  * packet attributes used by queueing disciplines.
135  * pattr_class is a discipline-dependent scheduling class that is
136  * set by a classifier.
137  * pattr_hdr and pattr_af may be used by a discipline to access
138  * the header within a mbuf.  (e.g. ECN needs to update the CE bit)
139  * note that pattr_hdr could be stale after m_pullup, though link
140  * layer output routines usually don't use m_pullup.  link-level
141  * compression also invalidates these fields.  thus, pattr_hdr needs
142  * to be verified when a discipline touches the header.
143  */
144 struct altq_pktattr {
145         void    *pattr_class;           /* sched class set by classifier */
146         int     pattr_af;               /* address family */
147         caddr_t pattr_hdr;              /* saved header position in mbuf */
148 };
149
150 /*
151  * a token-bucket regulator limits the rate that a network driver can
152  * dequeue packets from the output queue.
153  * modern cards are able to buffer a large amount of packets and dequeue
154  * too many packets at a time.  this bursty dequeue behavior makes it
155  * impossible to schedule packets by queueing disciplines.
156  * a token-bucket is used to control the burst size in a device
157  * independent manner.
158  */
159 struct tb_regulator {
160         int64_t         tbr_rate;       /* (scaled) token bucket rate */
161         int64_t         tbr_depth;      /* (scaled) token bucket depth */
162
163         int64_t         tbr_token;      /* (scaled) current token */
164         int64_t         tbr_filluptime; /* (scaled) time to fill up bucket */
165         uint64_t        tbr_last;       /* last time token was updated */
166
167         int             tbr_lastop;     /* last dequeue operation type
168                                            needed for poll-and-dequeue */
169 };
170
171 /* if_altqflags */
172 #define ALTQF_READY      0x01   /* driver supports alternate queueing */
173 #define ALTQF_ENABLED    0x02   /* altq is in use */
174 #define ALTQF_CLASSIFY   0x04   /* classify packets */
175 #define ALTQF_DRIVER1    0x40   /* driver specific */
176
177 /* if_altqflags set internally only: */
178 #define ALTQF_CANTCHANGE        (ALTQF_READY)
179
180 /* altq_dequeue 2nd arg */
181 #define ALTDQ_REMOVE            1       /* dequeue mbuf from the queue */
182 #define ALTDQ_POLL              2       /* don't dequeue mbuf from the queue */
183
184 /* altq request types (currently only purge is defined) */
185 #define ALTRQ_PURGE             1       /* purge all packets */
186
187 int     altq_attach(struct ifaltq *, int, void *,
188             ifsq_enqueue_t, ifsq_dequeue_t, ifsq_request_t, void *,
189             void *(*)(struct ifaltq *, struct mbuf *, struct altq_pktattr *));
190 int     altq_detach(struct ifaltq *);
191 int     altq_enable(struct ifaltq *);
192 int     altq_disable(struct ifaltq *);
193 struct mbuf *tbr_dequeue(struct ifaltq_subque *, struct mbuf *, int);
194 extern int      (*altq_input)(struct mbuf *, int);
195 #endif /* _KERNEL */
196
197 #endif /* _NET_ALTQ_IF_ALTQ_H_ */