FreeBSD/Linux Kernel Cross Reference
sys/netatm/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$
27 *
28 */
29
30 /*
31 * System Configuration
32 * --------------------
33 *
34 * Porting aides
35 *
36 */
37
38 #ifndef _NETATM_PORT_H
39 #define _NETATM_PORT_H
40
41 /*
42 * Try to ensure that this system is supported
43 */
44 #if (defined(BSD) && (BSD >= 199103))
45
46 /* 4.3 BSD Net2 based */
47
48 #elif defined(sun)
49
50 /* SunOS4.x */
51
52 #else
53
54 /* Ooops */
55 #error "Undefined/unsupported system type"
56
57 #endif
58
59
60 /*
61 * Kernel memory management
62 *
63 * KM_ALLOC(size, type, flags)
64 * Returns an allocated kernel memory chunk of size bytes.
65 * KM_FREE(addr, size, type)
66 * Free a kernel memory chunk of size bytes.
67 * KM_CMP(b1, b2, len)
68 * Compares len bytes of data from b1 against b2.
69 * KM_COPY(from, to, len)
70 * Copies len bytes of data from from to to.
71 * KM_ZERO(addr, len)
72 * Zeros len bytes of data from addr.
73 *
74 */
75 #ifdef ATM_KERNEL
76 #if (defined(BSD) && (BSD >= 199103))
77 #include <sys/malloc.h>
78 #define KM_ALLOC(size, type, flags) malloc((size), (type), (flags))
79 #define KM_FREE(addr, size, type) free((addr), (type))
80 #elif defined(sun)
81 #include <sys/kmem_alloc.h>
82 #define KM_ALLOC(size, type, flags) kmem_alloc(size)
83 #define KM_FREE(addr, size, type) kmem_free((addr), (size))
84 #endif
85
86 #if defined(BSD)
87 #define KM_CMP(b1, b2, len) bcmp((void *)(b1), (void *)(b2),\
88 (len))
89 #define KM_COPY(from, to, len) bcopy((void *)(from), (void *)(to),\
90 (len))
91 #define KM_ZERO(addr, len) bzero((void *)(addr), (len))
92 #endif
93 #define XM_COPY(f, t, l) KM_COPY((f), (t), (l))
94
95 #else
96
97 /*
98 * User-space memory management
99 *
100 * UM_ALLOC(size) Returns an allocated kernel memory chunk of size bytes.
101 * UM_FREE(addr) Free a kernel memory chunk of size bytes.
102 * UM_COPY(from, to, len)
103 * Copies len bytes of data from from to to.
104 * UM_ZERO(addr, len) Zeros len bytes of data from addr.
105 *
106 */
107 #if (defined(BSD) && (BSD >= 199103))
108 #define UM_ALLOC(size) malloc((size_t)(size))
109 #define UM_FREE(addr) free((void *)(addr))
110 #define UM_COPY(from, to, len) bcopy((void *)(from), (void *)(to),\
111 (size_t)(len))
112 #define UM_ZERO(addr, len) bzero((void *)(addr), (size_t)(len))
113 #elif defined(sun)
114 #define UM_ALLOC(size) malloc(size)
115 #define UM_FREE(addr) free((char *)(addr))
116 #define UM_COPY(from, to, len) bcopy((char *)(from), (char *)(to), (len))
117 #define UM_ZERO(addr, len) bzero((char *)(addr), (len))
118
119 #endif
120 #define XM_COPY(f, t, l) UM_COPY((f), (t), (l))
121
122 #endif /* ATM_KERNEL */
123
124
125 #ifdef ATM_KERNEL
126 /*
127 * Kernel buffers
128 *
129 * KBuffer Typedef for a kernel buffer.
130 *
131 * KB_NEXT(bfr) Access next buffer in chain (r/w).
132 * KB_LEN(bfr) Access length of data in this buffer (r/w).
133 * KB_QNEXT(bfr) Access next buffer in queue (r/w).
134 *
135 * KB_ALLOC(bfr, size, flags, type)
136 * Allocates a new kernel buffer of at least size bytes.
137 * KB_ALLOCPKT(bfr, size, flags, type)
138 * Allocates a new kernel packet header buffer of at
139 * least size bytes.
140 * KB_ALLOCEXT(bfr, size, flags, type)
141 * Allocates a new kernel buffer with external storage
142 * of at least size bytes.
143 * KB_FREEONE(bfr, nxt) Free buffer bfr and set next buffer in chain in nxt.
144 * KB_FREEALL(bfr) Free bfr's entire buffer chain.
145 * KB_COPY(bfr, off, len, new, flags)
146 * Copy len bytes of user data from buffer bfr starting at
147 * byte offset off and return new buffer chain in new.
148 * If len is KB_COPYALL, copy until end of chain.
149 * KB_COPYDATA(bfr, off, len, datap)
150 * Copy data from buffer bfr starting at byte offset off
151 * for len bytes into the data area pointed to by datap.
152 * Returns the number of bytes not copied to datap.
153 * KB_PULLUP(bfr, n, new)
154 * Get at least the first n bytes of data in the buffer
155 * chain headed by bfr contiguous in the first buffer.
156 * Returns the (potentially new) head of the chain in new.
157 * On failure the chain is freed and NULL is returned.
158 * KB_LINKHEAD(new, head)
159 * Link the kernel buffer new at the head of the buffer
160 * chain headed by head. If both new and head are
161 * packet header buffers, new will become the packet
162 * header for the chain.
163 * KB_LINK(new, prev)
164 * Link the kernel buffer new into the buffer chain
165 * after the buffer prev.
166 * KB_UNLINKHEAD(head, next)
167 * Unlink the kernel buffer from the head of the buffer
168 * chain headed by head. The buffer head will be freed
169 * and the new chain head will be placed in next.
170 * KB_UNLINK(old, prev, next)
171 * Unlink the kernel buffer old with previous buffer prev
172 * from its buffer chain. The following buffer in the
173 * chain will be placed in next and the buffer old will
174 * be freed.
175 * KB_ISPKT(bfr) Tests whether bfr is a packet header buffer.
176 * KB_ISEXT(bfr) Tests whether bfr has external storage.
177 * KB_BFRSTART(bfr, x, t)
178 * Sets x (cast to type t) to point to the start of the
179 * buffer space in bfr.
180 * KB_BFREND(bfr, x, t)
181 * Sets x (cast to type t) to point one byte past the end
182 * of the buffer space in bfr.
183 * KB_BFRLEN(bfr) Returns length of buffer space in bfr.
184 * KB_DATASTART(bfr, x, t)
185 * Sets x (cast to type t) to point to the start of the
186 * buffer data contained in bfr.
187 * KB_DATAEND(bfr, x, t)
188 * Sets x (cast to type t) to point one byte past the end
189 * of the buffer data contained in bfr.
190 * KB_HEADSET(bfr, n) Sets the start address for buffer data in buffer bfr to
191 * n bytes from the beginning of the buffer space.
192 * KB_HEADMOVE(bfr, n) Adjust buffer data controls to move data down (n > 0)
193 * or up (n < 0) n bytes in the buffer bfr.
194 * KB_HEADADJ(bfr, n) Adjust buffer data controls to add (n > 0) or subtract
195 * (n < 0) n bytes of data to/from the beginning of bfr.
196 * KB_TAILADJ(bfr, n) Adjust buffer data controls to add (n > 0) or subtract
197 * (n < 0) n bytes of data to/from the end of bfr.
198 * KB_TAILALIGN(bfr, n) Set buffer data controls to place an object of size n
199 * at the end of bfr, longword aligned.
200 * KB_HEADROOM(bfr, n) Set n to the amount of buffer space available before
201 * the start of data in bfr.
202 * KB_TAILROOM(bfr, n) Set n to the amount of buffer space available after
203 * the end of data in bfr.
204 * KB_PLENGET(bfr, n) Set n to bfr's packet length.
205 * KB_PLENSET(bfr, n) Set bfr's packet length to n.
206 * KB_PLENADJ(bfr, n) Adjust total packet length by n bytes.
207 *
208 */
209 #if defined(BSD)
210 #include <sys/mbuf.h>
211 typedef struct mbuf KBuffer;
212
213 #define KB_F_WAIT M_WAIT
214 #define KB_F_NOWAIT M_DONTWAIT
215
216 #define KB_T_HEADER MT_HEADER
217 #define KB_T_DATA MT_DATA
218
219 #define KB_COPYALL M_COPYALL
220
221 #if BSD >= 199103
222
223 #define KB_NEXT(bfr) (bfr)->m_next
224 #define KB_LEN(bfr) (bfr)->m_len
225 #define KB_QNEXT(bfr) (bfr)->m_nextpkt
226 #define KB_ALLOC(bfr, size, flags, type) { \
227 if ((size) <= MLEN) { \
228 MGET((bfr), (flags), (type)); \
229 } else \
230 (bfr) = NULL; \
231 }
232 #define KB_ALLOCPKT(bfr, size, flags, type) { \
233 if ((size) <= MHLEN) { \
234 MGETHDR((bfr), (flags), (type)); \
235 } else \
236 (bfr) = NULL; \
237 }
238 #define KB_ALLOCEXT(bfr, size, flags, type) { \
239 if ((size) <= MCLBYTES) { \
240 MGET((bfr), (flags), (type)); \
241 if ((bfr) != NULL) { \
242 MCLGET((bfr), (flags)); \
243 if (((bfr)->m_flags & M_EXT) == 0) { \
244 m_freem((bfr)); \
245 (bfr) = NULL; \
246 } \
247 } \
248 } else \
249 (bfr) = NULL; \
250 }
251 #define KB_FREEONE(bfr, nxt) { \
252 (nxt) = m_free(bfr); \
253 }
254 #define KB_FREEALL(bfr) { \
255 m_freem(bfr); \
256 }
257 #define KB_COPY(bfr, off, len, new, flags) { \
258 (new) = m_copym((bfr), (off), (len), (flags)); \
259 }
260 #define KB_COPYDATA(bfr, off, len, datap) \
261 (m_copydata((bfr), (off), (len), (datap)), 0)
262 #define KB_PULLUP(bfr, n, new) { \
263 (new) = m_pullup((bfr), (n)); \
264 }
265 #define KB_LINKHEAD(new, head) { \
266 if ((head) && KB_ISPKT(new) && KB_ISPKT(head)) {\
267 M_MOVE_PKTHDR((new), (head)); \
268 } \
269 (new)->m_next = (head); \
270 }
271 #define KB_LINK(new, prev) { \
272 (new)->m_next = (prev)->m_next; \
273 (prev)->m_next = (new); \
274 }
275 #define KB_UNLINKHEAD(head, next) { \
276 next = m_free((head)); \
277 }
278 #define KB_UNLINK(old, prev, next) { \
279 next = m_free((old)); \
280 (prev)->m_next = (next); \
281 }
282 #define KB_ISPKT(bfr) (((bfr)->m_flags & M_PKTHDR) != 0)
283 #define KB_ISEXT(bfr) (((bfr)->m_flags & M_EXT) != 0)
284 #define KB_BFRSTART(bfr, x, t) { \
285 if ((bfr)->m_flags & M_EXT) \
286 (x) = (t)((bfr)->m_ext.ext_buf); \
287 else if ((bfr)->m_flags & M_PKTHDR) \
288 (x) = (t)(&(bfr)->m_pktdat); \
289 else \
290 (x) = (t)((bfr)->m_dat); \
291 }
292 #define KB_BFREND(bfr, x, t) { \
293 if ((bfr)->m_flags & M_EXT) \
294 (x) = (t)((bfr)->m_ext.ext_buf + (bfr)->m_ext.ext_size);\
295 else if ((bfr)->m_flags & M_PKTHDR) \
296 (x) = (t)(&(bfr)->m_pktdat + MHLEN); \
297 else \
298 (x) = (t)((bfr)->m_dat + MLEN); \
299 }
300 #define KB_BFRLEN(bfr) \
301 (((bfr)->m_flags & M_EXT) ? (bfr)->m_ext.ext_size : \
302 (((bfr)->m_flags & M_PKTHDR) ? MHLEN : MLEN))
303 #define KB_DATASTART(bfr, x, t) { \
304 (x) = mtod((bfr), t); \
305 }
306 #define KB_DATAEND(bfr, x, t) { \
307 (x) = (t)(mtod((bfr), caddr_t) + (bfr)->m_len); \
308 }
309 #define KB_HEADSET(bfr, n) { \
310 if ((bfr)->m_flags & M_EXT) \
311 (bfr)->m_data = (bfr)->m_ext.ext_buf + (n); \
312 else if ((bfr)->m_flags & M_PKTHDR) \
313 (bfr)->m_data = (bfr)->m_pktdat + (n); \
314 else \
315 (bfr)->m_data = (bfr)->m_dat + (n); \
316 }
317 #define KB_HEADMOVE(bfr, n) { \
318 (bfr)->m_data += (n); \
319 }
320 #define KB_HEADADJ(bfr, n) { \
321 (bfr)->m_len += (n); \
322 (bfr)->m_data -= (n); \
323 }
324 #define KB_TAILADJ(bfr, n) { \
325 (bfr)->m_len += (n); \
326 }
327 #define KB_TAILALIGN(bfr, n) { \
328 (bfr)->m_len = (n); \
329 if ((bfr)->m_flags & M_EXT) \
330 (bfr)->m_data = (caddr_t)(((u_int)(bfr)->m_ext.ext_buf \
331 + (bfr)->m_ext.ext_size - (n)) & ~(sizeof(long) - 1));\
332 else \
333 (bfr)->m_data = (caddr_t)(((u_int)(bfr)->m_dat + MLEN - (n)) \
334 & ~(sizeof(long) - 1)); \
335 }
336 #define KB_HEADROOM(bfr, n) { \
337 /* n = M_LEADINGSPACE(bfr) XXX */ \
338 (n) = ((bfr)->m_flags & M_EXT ? (bfr)->m_data - (bfr)->m_ext.ext_buf : \
339 (bfr)->m_flags & M_PKTHDR ? (bfr)->m_data - (bfr)->m_pktdat : \
340 (bfr)->m_data - (bfr)->m_dat); \
341 }
342 #define KB_TAILROOM(bfr, n) { \
343 (n) = M_TRAILINGSPACE(bfr); \
344 }
345 #define KB_PLENGET(bfr, n) { \
346 (n) = (bfr)->m_pkthdr.len; \
347 }
348 #define KB_PLENSET(bfr, n) { \
349 (bfr)->m_pkthdr.len = (n); \
350 }
351 #define KB_PLENADJ(bfr, n) { \
352 (bfr)->m_pkthdr.len += (n); \
353 }
354
355
356 #else /* ! BSD >= 199103 */
357
358
359 #define KB_NEXT(bfr) (bfr)->m_next
360 #define KB_LEN(bfr) (bfr)->m_len
361 #define KB_QNEXT(bfr) (bfr)->m_act
362 #define KB_ALLOC(bfr, size, flags, type) { \
363 if ((size) <= MLEN) { \
364 MGET((bfr), (flags), (type)); \
365 } else \
366 (bfr) = NULL; \
367 }
368 #define KB_ALLOCPKT(bfr, size, flags, type) { \
369 if ((size) <= MLEN) { \
370 MGET((bfr), (flags), (type)); \
371 } else \
372 (bfr) = NULL; \
373 }
374 #define KB_ALLOCEXT(bfr, size, flags, type) { \
375 if ((size) <= MCLBYTES) { \
376 MGET((bfr), (flags), (type)); \
377 if ((bfr) != NULL) { \
378 MCLGET(bfr); \
379 if ((bfr)->m_len != MCLBYTES) { \
380 m_freem((bfr)); \
381 (bfr) = NULL; \
382 } \
383 } \
384 } else \
385 (bfr) = NULL; \
386 }
387 #define KB_FREEONE(bfr, nxt) { \
388 (nxt) = m_free(bfr); \
389 }
390 #define KB_FREEALL(bfr) { \
391 m_freem(bfr); \
392 }
393 #define KB_COPY(bfr, off, len, new, flags) { \
394 (new) = m_copy((bfr), (off), (len)); \
395 }
396 #define KB_COPYDATA(bfr, off, len, datap) \
397 m_cpytoc((bfr), (off), (len), (datap))
398 #define KB_PULLUP(bfr, n, new) { \
399 (new) = m_pullup((bfr), (n)); \
400 }
401 #define KB_LINKHEAD(new, head) { \
402 (new)->m_next = (head); \
403 }
404 #define KB_LINK(new, prev) { \
405 (new)->m_next = (prev)->m_next; \
406 (prev)->m_next = (new); \
407 }
408 #define KB_UNLINKHEAD(head, next) { \
409 next = m_free((head)); \
410 }
411 #define KB_UNLINK(old, prev, next) { \
412 next = m_free((old)); \
413 (prev)->m_next = (next); \
414 }
415 #define KB_ISPKT(bfr) (0)
416 #define KB_ISEXT(bfr) M_HASCL(bfr)
417 #define KB_BFRSTART(bfr, x, t) { \
418 if (M_HASCL(bfr)) { \
419 if ((bfr)->m_cltype == MCL_STATIC) \
420 (x) = (t)(mtod((bfr), int) & ~(MCLBYTES - 1)); \
421 else \
422 (x) = (t)NULL; \
423 } else \
424 (x) = (t)((bfr)->m_dat); \
425 }
426 #define KB_BFREND(bfr, x, t) { \
427 if (M_HASCL(bfr)) { \
428 if ((bfr)->m_cltype == MCL_STATIC) \
429 (x) = (t)((mtod((bfr), int) & ~(MCLBYTES - 1)) \
430 + MCLBYTES); \
431 else \
432 (x) = (t)NULL; \
433 } else \
434 (x) = (t)((bfr)->m_dat + MLEN); \
435 }
436 #define KB_BFRLEN(bfr) \
437 (M_HASCL(bfr) ? (((bfr)->m_cltype == MCL_STATIC) ? MCLBYTES : 0) : MLEN)
438 #define KB_DATASTART(bfr, x, t) { \
439 (x) = mtod((bfr), t); \
440 }
441 #define KB_DATAEND(bfr, x, t) { \
442 (x) = (t)(mtod((bfr), caddr_t) + (bfr)->m_len); \
443 }
444 #define KB_HEADSET(bfr, n) { \
445 if (M_HASCL(bfr)) { \
446 /* Assume cluster buffer is empty XXX */\
447 (bfr)->m_off += (n); \
448 } else \
449 (bfr)->m_off = MMINOFF + (n); \
450 }
451 #define KB_HEADMOVE(bfr, n) { \
452 (bfr)->m_off += (n); \
453 }
454 #define KB_HEADADJ(bfr, n) { \
455 (bfr)->m_len += (n); \
456 (bfr)->m_off -= (n); \
457 }
458 #define KB_TAILADJ(bfr, n) { \
459 (bfr)->m_len += (n); \
460 }
461 #define KB_TAILALIGN(bfr, n) { \
462 (bfr)->m_len = (n); \
463 if (M_HASCL(bfr)) { \
464 if ((bfr)->m_cltype == MCL_STATIC) \
465 (bfr)->m_off = (int)(((mtod((bfr), int) \
466 & ~(MCLBYTES - 1)) + MCLBYTES - (n)) \
467 & ~(sizeof(long) - 1)) - (int)(bfr); \
468 /* Out of luck for loaned buffers */ \
469 } else \
470 (bfr)->m_off = (MMAXOFF - (n)) & ~(sizeof(long) - 1); \
471 }
472 #define KB_HEADROOM(bfr, n) { \
473 if (M_HASCL(bfr)) { \
474 if ((bfr)->m_cltype == MCL_STATIC) \
475 (n) = mtod((bfr), int) & (MCLBYTES - 1); \
476 else \
477 (n) = 0; \
478 } else \
479 (n) = (bfr)->m_off - MMINOFF; \
480 }
481 #define KB_TAILROOM(bfr, n) { \
482 if (M_HASCL(bfr)) { \
483 if ((bfr)->m_cltype == MCL_STATIC) \
484 (n) = MCLBYTES - ((mtod((bfr), int) + (bfr)->m_len) \
485 & (MCLBYTES - 1)); \
486 else \
487 (n) = 0; \
488 } else \
489 (n) = MMAXOFF - ((bfr)->m_off + (bfr)->m_len); \
490 }
491 #define KB_PLENGET(bfr, n) { \
492 struct mbuf *zz; \
493 for ((n) = 0, zz = (bfr); zz; zz = zz->m_next) \
494 (n) += zz->m_len; \
495 }
496 #define KB_PLENSET(bfr, n) { \
497 }
498 #define KB_PLENADJ(bfr, n) { \
499 }
500
501
502 #endif /* ! BSD >= 199103 */
503
504 #endif /* defined(BSD) */
505
506
507 /*
508 * Kernel time
509 *
510 * KTimeout_ret Typedef for timeout() function return
511 *
512 * KT_TIME(t) Sets t to the current time.
513 *
514 */
515 #if (defined(BSD) && (BSD >= 199306))
516 typedef void KTimeout_ret;
517 #else
518 typedef int KTimeout_ret;
519 #endif
520 #if (defined(BSD) && (BSD >= 199103))
521 #define KT_TIME(t) microtime(&t)
522 #elif defined(sun)
523 #define KT_TIME(t) uniqtime(&t)
524 #else
525 #define KT_TIME(t) ((t) = time)
526 #endif
527
528 #endif /* ATM_KERNEL */
529
530 #ifndef NTOHL
531 #if BYTE_ORDER == BIG_ENDIAN
532 #define NTOHL(x) (x)
533 #define NTOHS(x) (x)
534 #define HTONL(x) (x)
535 #define HTONS(x) (x)
536 #else
537 #define NTOHL(x) (x) = ntohl((u_long)(x))
538 #define NTOHS(x) (x) = ntohs((u_short)(x))
539 #define HTONL(x) (x) = htonl((u_long)(x))
540 #define HTONS(x) (x) = htons((u_short)(x))
541 #endif
542 #endif /* NTOHL */
543
544 #ifndef MAX
545 #define MAX(a,b) max((a),(b))
546 #endif
547 #ifndef MIN
548 #define MIN(a,b) min((a),(b))
549 #endif
550
551 #if (!(defined(BSD) && (BSD >= 199306)))
552 #ifndef __BIT_TYPES_DEFINED__
553 #define __BIT_TYPES_DEFINED__
554 typedef char int8_t;
555 typedef unsigned char u_int8_t;
556 typedef short int16_t;
557 typedef unsigned short u_int16_t;
558 typedef int int32_t;
559 typedef unsigned int u_int32_t;
560 #endif
561 #endif
562
563 #endif /* _NETATM_PORT_H */
Cache object: 22876ea35247b7d73e0518881bee65e7
|