Merge from vendor branch BIND:
[dragonfly.git] / sys / netproto / atm / port.h
1 /*
2  *
3  * ===================================
4  * HARP  |  Host ATM Research Platform
5  * ===================================
6  *
7  *
8  * This Host ATM Research Platform ("HARP") file (the "Software") is
9  * made available by Network Computing Services, Inc. ("NetworkCS")
10  * "AS IS".  NetworkCS does not provide maintenance, improvements or
11  * support of any kind.
12  *
13  * NETWORKCS MAKES NO WARRANTIES OR REPRESENTATIONS, EXPRESS OR IMPLIED,
14  * INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
15  * AND FITNESS FOR A PARTICULAR PURPOSE, AS TO ANY ELEMENT OF THE
16  * SOFTWARE OR ANY SUPPORT PROVIDED IN CONNECTION WITH THIS SOFTWARE.
17  * In no event shall NetworkCS be responsible for any damages, including
18  * but not limited to consequential damages, arising from or relating to
19  * any use of the Software or related support.
20  *
21  * Copyright 1994-1998 Network Computing Services, Inc.
22  *
23  * Copies of this Software may be made, however, the above copyright
24  * notice must be reproduced on all copies.
25  *
26  *      @(#) $FreeBSD: src/sys/netatm/port.h,v 1.2.2.2 2003/01/23 21:06:44 sam Exp $
27  *      @(#) $DragonFly: src/sys/netproto/atm/port.h,v 1.7 2005/02/01 00:51:50 joerg Exp $
28  *
29  */
30
31 /*
32  * System Configuration
33  * --------------------
34  *
35  * Porting aides
36  *
37  */
38
39 #ifndef _NETATM_PORT_H
40 #define _NETATM_PORT_H
41
42 /*
43  * Kernel memory management
44  *
45  * KM_ALLOC(size, type, flags)
46  *                      Returns an allocated kernel memory chunk of size bytes.
47  * KM_FREE(addr, size, type)
48  *                      Free a kernel memory chunk of size bytes.
49  * KM_CMP(b1, b2, len)
50  *                      Compares len bytes of data from b1 against b2.
51  * KM_COPY(from, to, len)
52  *                      Copies len bytes of data from from to to.
53  * KM_ZERO(addr, len)
54  *                      Zeros len bytes of data from addr.
55  *
56  */
57 #ifdef ATM_KERNEL
58 #include <sys/malloc.h>
59 #define KM_ALLOC(size, type, flags)     malloc((size), (type), (flags))
60 #define KM_FREE(addr, size, type)       free((addr), (type))
61
62 #define KM_CMP(b1, b2, len)             bcmp((b1), (b2), (len))
63 #define KM_COPY(from, to, len)          bcopy((from), (to), (len))
64 #define KM_ZERO(addr, len)              bzero((addr), (len))
65 #define XM_COPY(f, t, l)        KM_COPY((f), (t), (l))
66
67 #else
68
69 /*
70  * User-space memory management
71  *
72  * UM_ALLOC(size)       Returns an allocated kernel memory chunk of size bytes.
73  * UM_FREE(addr)        Free a kernel memory chunk of size bytes.
74  * UM_COPY(from, to, len)
75  *                      Copies len bytes of data from from to to.
76  * UM_ZERO(addr, len)   Zeros len bytes of data from addr.
77  *
78  */
79 #define UM_ALLOC(size)          malloc((size_t)(size))
80 #define UM_FREE(addr)           free((void *)(addr))
81 #define UM_COPY(from, to, len)  bcopy((void *)(from), (void *)(to),\
82                                                 (size_t)(len))
83 #define UM_ZERO(addr, len)      bzero((void *)(addr), (size_t)(len))
84 #define XM_COPY(f, t, l)        UM_COPY((f), (t), (l))
85
86 #endif  /* ATM_KERNEL */
87
88
89 #ifdef ATM_KERNEL
90 /*
91  * Kernel buffers
92  *
93  * KBuffer              Typedef for a kernel buffer.
94  *
95  * KB_NEXT(bfr)         Access next buffer in chain (r/w).
96  * KB_LEN(bfr)          Access length of data in this buffer (r/w).
97  * KB_QNEXT(bfr)        Access next buffer in queue (r/w).
98  *
99  * KB_ALLOC(bfr, size, flags, type)
100  *                      Allocates a new kernel buffer of at least size bytes.
101  * KB_ALLOCPKT(bfr, size, flags, type)
102  *                      Allocates a new kernel packet header buffer of at
103  *                      least size bytes.
104  * KB_ALLOCEXT(bfr, size, flags, type)
105  *                      Allocates a new kernel buffer with external storage
106  *                      of at least size bytes.
107  * KB_FREEONE(bfr, nxt) Free buffer bfr and set next buffer in chain in nxt.
108  * KB_FREEALL(bfr)      Free bfr's entire buffer chain.
109  * KB_COPY(bfr, off, len, new, flags)
110  *                      Copy len bytes of user data from buffer bfr starting at
111  *                      byte offset off and return new buffer chain in new.
112  *                      If len is KB_COPYALL, copy until end of chain.
113  * KB_COPYDATA(bfr, off, len, datap)
114  *                      Copy data from buffer bfr starting at byte offset off
115  *                      for len bytes into the data area pointed to by datap.
116  *                      Returns the number of bytes not copied to datap.
117  * KB_PULLUP(bfr, n, new)
118  *                      Get at least the first n bytes of data in the buffer 
119  *                      chain headed by bfr contiguous in the first buffer.
120  *                      Returns the (potentially new) head of the chain in new.
121  *                      On failure the chain is freed and NULL is returned.
122  * KB_LINKHEAD(new, head)
123  *                      Link the kernel buffer new at the head of the buffer
124  *                      chain headed by head.  If both new and head are
125  *                      packet header buffers, new will become the packet
126  *                      header for the chain.
127  * KB_LINK(new, prev)
128  *                      Link the kernel buffer new into the buffer chain
129  *                      after the buffer prev.
130  * KB_UNLINKHEAD(head, next)
131  *                      Unlink the kernel buffer from the head of the buffer
132  *                      chain headed by head.  The buffer head will be freed
133  *                      and the new chain head will be placed in next.
134  * KB_UNLINK(old, prev, next)
135  *                      Unlink the kernel buffer old with previous buffer prev
136  *                      from its buffer chain.  The following buffer in the
137  *                      chain will be placed in next and the buffer old will
138  *                      be freed.
139  * KB_ISPKT(bfr)        Tests whether bfr is a packet header buffer.
140  * KB_ISEXT(bfr)        Tests whether bfr has external storage.
141  * KB_BFRSTART(bfr, x, t)
142  *                      Sets x (cast to type t) to point to the start of the
143  *                      buffer space in bfr.
144  * KB_BFREND(bfr, x, t)
145  *                      Sets x (cast to type t) to point one byte past the end
146  *                      of the buffer space in bfr.
147  * KB_BFRLEN(bfr)       Returns length of buffer space in bfr.
148  * KB_DATASTART(bfr, x, t)
149  *                      Sets x (cast to type t) to point to the start of the
150  *                      buffer data contained in bfr.
151  * KB_DATAEND(bfr, x, t)
152  *                      Sets x (cast to type t) to point one byte past the end
153  *                      of the buffer data contained in bfr.
154  * KB_HEADSET(bfr, n)   Sets the start address for buffer data in buffer bfr to
155  *                      n bytes from the beginning of the buffer space.
156  * KB_HEADMOVE(bfr, n)  Adjust buffer data controls to move data down (n > 0) 
157  *                      or up (n < 0) n bytes in the buffer bfr.
158  * KB_HEADADJ(bfr, n)   Adjust buffer data controls to add (n > 0) or subtract
159  *                      (n < 0) n bytes of data to/from the beginning of bfr.
160  * KB_TAILADJ(bfr, n)   Adjust buffer data controls to add (n > 0) or subtract
161  *                      (n < 0) n bytes of data to/from the end of bfr.
162  * KB_TAILALIGN(bfr, n) Set buffer data controls to place an object of size n
163  *                      at the end of bfr, longword aligned.
164  * KB_HEADROOM(bfr, n)  Set n to the amount of buffer space available before
165  *                      the start of data in bfr.
166  * KB_TAILROOM(bfr, n)  Set n to the amount of buffer space available after
167  *                      the end of data in bfr.
168  * KB_PLENGET(bfr, n)   Set n to bfr's packet length.
169  * KB_PLENSET(bfr, n)   Set bfr's packet length to n.
170  * KB_PLENADJ(bfr, n)   Adjust total packet length by n bytes.
171  *
172  */
173 #include <sys/mbuf.h>
174 typedef struct mbuf     KBuffer;
175
176 #define KB_F_WAIT       MB_WAIT
177 #define KB_F_NOWAIT     MB_DONTWAIT
178
179 #define KB_T_HEADER     MT_HEADER
180 #define KB_T_DATA       MT_DATA
181
182 #define KB_COPYALL      M_COPYALL
183
184 #define KB_NEXT(bfr)            (bfr)->m_next
185 #define KB_LEN(bfr)             (bfr)->m_len
186 #define KB_QNEXT(bfr)           (bfr)->m_nextpkt
187 #define KB_ALLOC(bfr, size, flags, type) {              \
188         if ((size) <= MLEN) {                           \
189                 MGET((bfr), (flags), (type));           \
190         } else                                          \
191                 (bfr) = NULL;                           \
192 }
193 #define KB_ALLOCPKT(bfr, size, flags, type) {           \
194         if ((size) <= MHLEN) {                          \
195                 MGETHDR((bfr), (flags), (type));        \
196         } else                                          \
197                 (bfr) = NULL;                           \
198 }
199 #define KB_ALLOCEXT(bfr, size, flags, type) {           \
200         if ((size) <= MCLBYTES) {                       \
201                 MGET((bfr), (flags), (type));           \
202                 if ((bfr) != NULL) {                    \
203                         MCLGET((bfr), (flags));         \
204                         if (((bfr)->m_flags & M_EXT) == 0) {    \
205                                 m_freem((bfr));         \
206                                 (bfr) = NULL;           \
207                         }                               \
208                 }                                       \
209         } else                                          \
210                 (bfr) = NULL;                           \
211 }
212 #define KB_FREEONE(bfr, nxt) {                          \
213         (nxt) = m_free(bfr);                            \
214 }
215 #define KB_FREEALL(bfr) {                               \
216         m_freem(bfr);                                   \
217 }
218 #define KB_COPY(bfr, off, len, new, flags) {            \
219         (new) = m_copym((bfr), (off), (len), (flags));  \
220 }
221 #define KB_COPYDATA(bfr, off, len, datap)               \
222         (m_copydata((bfr), (off), (len), (datap)), 0)
223 #define KB_PULLUP(bfr, n, new) {                        \
224         (new) = m_pullup((bfr), (n));                   \
225 }
226 #define KB_LINKHEAD(new, head) {                        \
227         if ((head) && KB_ISPKT(new) && KB_ISPKT(head)) {\
228                 M_MOVE_PKTHDR((new), (head));           \
229         }                                               \
230         (new)->m_next = (head);                         \
231 }
232 #define KB_LINK(new, prev) {                            \
233         (new)->m_next = (prev)->m_next;                 \
234         (prev)->m_next = (new);                         \
235 }
236 #define KB_UNLINKHEAD(head, next) {                     \
237         next = m_free((head));                          \
238 }
239 #define KB_UNLINK(old, prev, next) {                    \
240         next = m_free((old));                           \
241         (prev)->m_next = (next);                        \
242 }
243 #define KB_ISPKT(bfr)           (((bfr)->m_flags & M_PKTHDR) != 0)
244 #define KB_ISEXT(bfr)           (((bfr)->m_flags & M_EXT) != 0)
245 #define KB_BFRSTART(bfr, x, t) {                        \
246         if ((bfr)->m_flags & M_EXT)                     \
247                 (x) = (t)((bfr)->m_ext.ext_buf);        \
248         else if ((bfr)->m_flags & M_PKTHDR)             \
249                 (x) = (t)(&(bfr)->m_pktdat);            \
250         else                                            \
251                 (x) = (t)((bfr)->m_dat);                \
252 }
253 #define KB_BFREND(bfr, x, t) {                          \
254         if ((bfr)->m_flags & M_EXT)                     \
255                 (x) = (t)((bfr)->m_ext.ext_buf + (bfr)->m_ext.ext_size);\
256         else if ((bfr)->m_flags & M_PKTHDR)             \
257                 (x) = (t)(&(bfr)->m_pktdat + MHLEN);    \
258         else                                            \
259                 (x) = (t)((bfr)->m_dat + MLEN);         \
260 }
261 #define KB_BFRLEN(bfr)                                  \
262         (((bfr)->m_flags & M_EXT) ? (bfr)->m_ext.ext_size :     \
263                 (((bfr)->m_flags & M_PKTHDR) ? MHLEN : MLEN))
264 #define KB_DATASTART(bfr, x, t) {                       \
265         (x) = mtod((bfr), t);                           \
266 }
267 #define KB_DATAEND(bfr, x, t) {                         \
268         (x) = (t)(mtod((bfr), caddr_t) + (bfr)->m_len); \
269 }
270 #define KB_HEADSET(bfr, n) {                            \
271         if ((bfr)->m_flags & M_EXT)                     \
272                 (bfr)->m_data = (bfr)->m_ext.ext_buf + (n);     \
273         else if ((bfr)->m_flags & M_PKTHDR)             \
274                 (bfr)->m_data = (bfr)->m_pktdat + (n);  \
275         else                                            \
276                 (bfr)->m_data = (bfr)->m_dat + (n);     \
277 }
278 #define KB_HEADMOVE(bfr, n) {                           \
279         (bfr)->m_data += (n);                           \
280 }
281 #define KB_HEADADJ(bfr, n) {                            \
282         (bfr)->m_len += (n);                            \
283         (bfr)->m_data -= (n);                           \
284 }
285 #define KB_TAILADJ(bfr, n) {                            \
286         (bfr)->m_len += (n);                            \
287 }
288 #define KB_TAILALIGN(bfr, n) {                          \
289         (bfr)->m_len = (n);                             \
290         if ((bfr)->m_flags & M_EXT)                     \
291                 (bfr)->m_data = (caddr_t)(((u_int)(bfr)->m_ext.ext_buf  \
292                         + (bfr)->m_ext.ext_size - (n)) & ~(sizeof(long) - 1));\
293         else                                            \
294                 (bfr)->m_data = (caddr_t)(((u_int)(bfr)->m_dat + MLEN - (n)) \
295                         & ~(sizeof(long) - 1));         \
296 }
297 #define KB_HEADROOM(bfr, n) {                           \
298         /* n = M_LEADINGSPACE(bfr) XXX */               \
299         (n) = ((bfr)->m_flags & M_EXT ? (bfr)->m_data - (bfr)->m_ext.ext_buf : \
300                 (bfr)->m_flags & M_PKTHDR ? (bfr)->m_data - (bfr)->m_pktdat : \
301                         (bfr)->m_data - (bfr)->m_dat);  \
302 }
303 #define KB_TAILROOM(bfr, n) {                           \
304         (n) = M_TRAILINGSPACE(bfr);                     \
305 }
306 #define KB_PLENGET(bfr, n) {                            \
307         (n) = (bfr)->m_pkthdr.len;                      \
308 }
309 #define KB_PLENSET(bfr, n) {                            \
310         (bfr)->m_pkthdr.len = (n);                      \
311 }
312 #define KB_PLENADJ(bfr, n) {                            \
313         (bfr)->m_pkthdr.len += (n);                     \
314 }
315
316 /*
317  * Kernel time
318  *
319  * KTimeout_ret         Typedef for timeout() function return
320  *
321  * KT_TIME(t)           Sets t to the current time.
322  *
323  */
324 typedef void    KTimeout_ret;
325 #define KT_TIME(t)      microtime(&t)
326
327 #endif  /* ATM_KERNEL */
328
329 #ifndef MAX
330 #define MAX(a,b)        max((a),(b))
331 #endif
332 #ifndef MIN
333 #define MIN(a,b)        min((a),(b))
334 #endif
335
336 #endif  /* _NETATM_PORT_H */