OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [ecos-2.0/] [packages/] [net/] [tcpip/] [v2_0/] [include/] [sys/] [mbuf.h] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1254 phoenix
//==========================================================================
2
//
3
//      include/sys/mbuf.h
4
//
5
//      
6
//
7
//==========================================================================
8
//####BSDCOPYRIGHTBEGIN####
9
//
10
// -------------------------------------------
11
//
12
// Portions of this software may have been derived from OpenBSD or other sources,
13
// and are covered by the appropriate copyright disclaimers included herein.
14
//
15
// -------------------------------------------
16
//
17
//####BSDCOPYRIGHTEND####
18
//==========================================================================
19
//#####DESCRIPTIONBEGIN####
20
//
21
// Author(s):    gthomas
22
// Contributors: gthomas
23
// Date:         2000-01-10
24
// Purpose:      
25
// Description:  
26
//              
27
//
28
//####DESCRIPTIONEND####
29
//
30
//==========================================================================
31
 
32
 
33
/*      $OpenBSD: mbuf.h,v 1.14 1999/12/08 06:50:24 itojun Exp $        */
34
/*      $NetBSD: mbuf.h,v 1.19 1996/02/09 18:25:14 christos Exp $       */
35
 
36
/*
37
 * Copyright (c) 1982, 1986, 1988, 1993
38
 *      The Regents of the University of California.  All rights reserved.
39
 *
40
 * Redistribution and use in source and binary forms, with or without
41
 * modification, are permitted provided that the following conditions
42
 * are met:
43
 * 1. Redistributions of source code must retain the above copyright
44
 *    notice, this list of conditions and the following disclaimer.
45
 * 2. Redistributions in binary form must reproduce the above copyright
46
 *    notice, this list of conditions and the following disclaimer in the
47
 *    documentation and/or other materials provided with the distribution.
48
 * 3. All advertising materials mentioning features or use of this software
49
 *    must display the following acknowledgement:
50
 *      This product includes software developed by the University of
51
 *      California, Berkeley and its contributors.
52
 * 4. Neither the name of the University nor the names of its contributors
53
 *    may be used to endorse or promote products derived from this software
54
 *    without specific prior written permission.
55
 *
56
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66
 * SUCH DAMAGE.
67
 *
68
 *      @(#)mbuf.h      8.5 (Berkeley) 2/19/95
69
 */
70
 
71
#ifndef _SYS_MBUF_H_
72
#define _SYS_MBUF_H_
73
 
74
#ifndef M_WAITOK
75
#include <sys/malloc.h>
76
#endif
77
 
78
/*
79
 * Mbufs are of a single size, MSIZE (machine/param.h), which
80
 * includes overhead.  An mbuf may add a single "mbuf cluster" of size
81
 * MCLBYTES (also in machine/param.h), which has no additional overhead
82
 * and is used instead of the internal data area; this is done when
83
 * at least MINCLSIZE of data must be stored.
84
 */
85
 
86
#define MLEN            (MSIZE - sizeof(struct m_hdr))  /* normal data len */
87
#define MHLEN           (MLEN - sizeof(struct pkthdr))  /* data len w/pkthdr */
88
 
89
#define MINCLSIZE       (MHLEN + 1)     /* smallest amount to put in cluster */
90
#define M_MAXCOMPRESS   (MHLEN / 2)     /* max amount to copy for compression */
91
 
92
/*
93
 * Macros for type conversion
94
 * mtod(m,t) -  convert mbuf pointer to data pointer of correct type
95
 * dtom(x) -    convert data pointer within mbuf to mbuf pointer (XXX)
96
 * mtocl(x) -   convert pointer within cluster to cluster index #
97
 * cltom(x) -   convert cluster # to ptr to beginning of cluster
98
 */
99
#define mtod(m,t)       ((t)((m)->m_data))
100
#define dtom(x)         ((struct mbuf *)((long)(x) & ~(MSIZE-1)))
101
#ifdef __ECOS
102
extern int cyg_mtocl(u_long);
103
extern struct mbuf *cyg_cltom(u_long);
104
#define mtocl(x) cyg_mtocl((u_long)x)
105
#define cltom(x) cyg_cltom((u_long)x)
106
#else
107
#define mtocl(x)        (((u_long)(x) - (u_long)mbutl) >> MCLSHIFT)
108
#define cltom(x)        ((caddr_t)((u_long)mbutl + ((u_long)(x) << MCLSHIFT)))
109
#endif
110
 
111
/* header at beginning of each mbuf: */
112
struct m_hdr {
113
        struct  mbuf *mh_next;          /* next buffer in chain */
114
        struct  mbuf *mh_nextpkt;       /* next chain in queue/record */
115
        caddr_t mh_data;                /* location of data */
116
        u_int   mh_len;                 /* amount of data in this mbuf */
117
        short   mh_type;                /* type of data in this mbuf */
118
        short   mh_flags;               /* flags; see below */
119
};
120
 
121
/* record/packet header in first mbuf of chain; valid if M_PKTHDR set */
122
struct  pkthdr {
123
        struct  ifnet *rcvif;           /* rcv interface */
124
        int     len;                    /* total packet length */
125
        void    *tdbi;                  /* pointer to struct tdb_ident */
126
                                        /* XXX - pull in ip_ipsp.h */
127
};
128
 
129
/* description of external storage mapped into mbuf, valid if M_EXT set */
130
struct m_ext {
131
        caddr_t ext_buf;                /* start of buffer */
132
        void    (*ext_free)             /* free routine if not the usual */
133
                    __P((struct mbuf *));
134
        u_int   ext_size;               /* size of buffer, for ext_free */
135
        void    (*ext_ref)              /* add a reference to the ext object */
136
                    __P((struct mbuf *));
137
        void    *ext_handle;            /* handle for storage manager */
138
};
139
 
140
struct mbuf {
141
        struct  m_hdr m_hdr;
142
        union {
143
                struct {
144
                        struct  pkthdr MH_pkthdr;       /* M_PKTHDR set */
145
                        union {
146
                                struct  m_ext MH_ext;   /* M_EXT set */
147
                                char    MH_databuf[MHLEN];
148
                        } MH_dat;
149
                } MH;
150
                char    M_databuf[MLEN];                /* !M_PKTHDR, !M_EXT */
151
        } M_dat;
152
};
153
#define m_next          m_hdr.mh_next
154
#define m_len           m_hdr.mh_len
155
#define m_data          m_hdr.mh_data
156
#define m_type          m_hdr.mh_type
157
#define m_flags         m_hdr.mh_flags
158
#define m_nextpkt       m_hdr.mh_nextpkt
159
#define m_act           m_nextpkt
160
#define m_pkthdr        M_dat.MH.MH_pkthdr
161
#define m_ext           M_dat.MH.MH_dat.MH_ext
162
#define m_pktdat        M_dat.MH.MH_dat.MH_databuf
163
#define m_dat           M_dat.M_databuf
164
 
165
/* mbuf flags */
166
#define M_EXT           0x0001  /* has associated external storage */
167
#define M_PKTHDR        0x0002  /* start of record */
168
#define M_EOR           0x0004  /* end of record */
169
 
170
/* mbuf pkthdr flags, also in m_flags */
171
#define M_BCAST         0x0100  /* send/received as link-level broadcast */
172
#define M_MCAST         0x0200  /* send/received as link-level multicast */
173
#define M_CONF          0x0400  /* packet was encrypted (ESP-transport) */
174
#define M_AUTH          0x0800  /* packet was authenticated (AH) */
175
#if 0 /* NRL IPv6 */
176
#define M_TUNNEL        0x1000  /* packet was tunneled */
177
#define M_DAD           0x2000  /* Used on outbound packets to indicate that
178
                                 * this is for duplicate address detection */
179
#endif
180
 
181
/* KAME IPv6 */
182
#define M_ANYCAST6      0x4000  /* received as IPv6 anycast */
183
#if 0 /*KAME IPSEC*/
184
#define M_AUTHIPHDR     0x0010  /* data origin authentication for IP header */
185
#define M_DECRYPTED     0x0020  /* confidentiality */
186
#endif
187
#define M_LOOP          0x0040  /* for Mbuf statistics */
188
#if 0 /*KAME IPSEC*/
189
#define M_AUTHIPDGM     0x0080  /* data origin authentication */
190
#endif
191
 
192
/* flags copied when copying m_pkthdr */
193
#define M_COPYFLAGS     (M_PKTHDR|M_EOR|M_BCAST|M_MCAST|M_CONF|M_AUTH|M_ANYCAST6|M_LOOP)
194
 
195
/* mbuf types */
196
#define MT_FREE         0        /* should be on free list */
197
#define MT_DATA         1       /* dynamic (data) allocation */
198
#define MT_HEADER       2       /* packet header */
199
#define MT_SOCKET       3       /* socket structure */
200
#define MT_PCB          4       /* protocol control block */
201
#define MT_RTABLE       5       /* routing tables */
202
#define MT_HTABLE       6       /* IMP host tables */
203
#define MT_ATABLE       7       /* address resolution tables */
204
#define MT_SONAME       8       /* socket name */
205
#define MT_SOOPTS       10      /* socket options */
206
#define MT_FTABLE       11      /* fragment reassembly header */
207
#define MT_RIGHTS       12      /* access rights */
208
#define MT_IFADDR       13      /* interface address */
209
#define MT_CONTROL      14      /* extra-data protocol message */
210
#define MT_OOBDATA      15      /* expedited data  */
211
 
212
/* flags to m_get/MGET */
213
#define M_DONTWAIT      M_NOWAIT
214
#define M_WAIT          M_WAITOK
215
 
216
#ifdef __ECOS
217
extern void *cyg_net_mbuf_alloc(int type, int flags);
218
extern void cyg_net_mbuf_free(caddr_t addr, int type);
219
#define MBUF_ALLOC(space, cast, size, type, flags) \
220
        (space) = (cast)cyg_net_mbuf_alloc(type, flags)
221
#define MBUF_FREE(addr, type) cyg_net_mbuf_free((caddr_t)(addr), type)
222
#else
223
#define MBUF_ALLOC(space, cast, size, type, flags) \
224
        MALLOC(space, cast, size, type, flags)
225
#define MBUF_FREE(addr, type) \
226
        MFREE(addr, type)
227
#endif
228
 
229
/*
230
 * mbuf utility macros:
231
 *
232
 *      MBUFLOCK(code)
233
 * prevents a section of code from from being interrupted by network
234
 * drivers.
235
 */
236
#define MBUFLOCK(code) \
237
        { int ms = splimp(); \
238
          { code } \
239
          splx(ms); \
240
        }
241
 
242
/*
243
 * mbuf allocation/deallocation macros:
244
 *
245
 *      MGET(struct mbuf *m, int how, int type)
246
 * allocates an mbuf and initializes it to contain internal data.
247
 *
248
 *      MGETHDR(struct mbuf *m, int how, int type)
249
 * allocates an mbuf and initializes it to contain a packet header
250
 * and internal data.
251
 */
252
#define MGET(m, how, type) { \
253
        MBUF_ALLOC((m), struct mbuf *, MSIZE, mbtypes[type], (how)); \
254
        if (m) { \
255
                (m)->m_type = (type); \
256
                MBUFLOCK(mbstat.m_mtypes[type]++;) \
257
                (m)->m_next = (struct mbuf *)NULL; \
258
                (m)->m_nextpkt = (struct mbuf *)NULL; \
259
                (m)->m_data = (m)->m_dat; \
260
                (m)->m_flags = 0; \
261
        } else \
262
                (m) = m_retry((how), (type)); \
263
}
264
 
265
#define MGETHDR(m, how, type) { \
266
        MBUF_ALLOC((m), struct mbuf *, MSIZE, mbtypes[type], (how)); \
267
        if (m) { \
268
                (m)->m_type = (type); \
269
                MBUFLOCK(mbstat.m_mtypes[type]++;) \
270
                (m)->m_next = (struct mbuf *)NULL; \
271
                (m)->m_nextpkt = (struct mbuf *)NULL; \
272
                (m)->m_data = (m)->m_pktdat; \
273
                (m)->m_flags = M_PKTHDR; \
274
        } else \
275
                (m) = m_retryhdr((how), (type)); \
276
}
277
 
278
/*
279
 * Mbuf cluster macros.
280
 * MCLALLOC(caddr_t p, int how) allocates an mbuf cluster.
281
 * MCLGET adds such clusters to a normal mbuf;
282
 * the flag M_EXT is set upon success.
283
 * MCLFREE releases a reference to a cluster allocated by MCLALLOC,
284
 * freeing the cluster if the reference count has reached 0.
285
 *
286
 * Normal mbuf clusters are normally treated as character arrays
287
 * after allocation, but use the first word of the buffer as a free list
288
 * pointer while on the free list.
289
 */
290
union mcluster {
291
        union   mcluster *mcl_next;
292
        char    mcl_buf[MCLBYTES];
293
};
294
 
295
#define MCLALLOC(p, how) \
296
        MBUFLOCK( \
297
          if (mclfree == 0) \
298
                (void)m_clalloc(1, (how)); \
299
          if (((p) = (caddr_t)mclfree) != 0) { \
300
                ++mclrefcnt[mtocl(p)]; \
301
                mbstat.m_clfree--; \
302
                mclfree = ((union mcluster *)(p))->mcl_next; \
303
          } \
304
        )
305
 
306
#define MCLGET(m, how) \
307
        { MCLALLOC((m)->m_ext.ext_buf, (how)); \
308
          if ((m)->m_ext.ext_buf != NULL) { \
309
                (m)->m_data = (m)->m_ext.ext_buf; \
310
                (m)->m_flags |= M_EXT; \
311
                (m)->m_ext.ext_size = MCLBYTES;  \
312
                (m)->m_ext.ext_free = NULL; \
313
                (m)->m_ext.ext_ref = NULL; \
314
                (m)->m_ext.ext_handle = NULL; \
315
          } \
316
        }
317
 
318
#define MCLFREE(p) \
319
        MBUFLOCK ( \
320
          if (--mclrefcnt[mtocl(p)] == 0) { \
321
                ((union mcluster *)(p))->mcl_next = mclfree; \
322
                mclfree = (union mcluster *)(p); \
323
                mbstat.m_clfree++; \
324
          } \
325
        )
326
 
327
/*
328
 * For cluster mbufs (regardless of header or not).
329
 */
330
#define MCL_ALIGN(m, len) \
331
        { (m)->m_data += (MCLBYTES - (len)) &~ (sizeof(long) -1); }
332
 
333
/*
334
 * MFREE(struct mbuf *m, struct mbuf *n)
335
 * Free a single mbuf and associated external storage.
336
 * Place the successor, if any, in n.
337
 */
338
#define MFREE(m, n) \
339
        { MBUFLOCK(mbstat.m_mtypes[(m)->m_type]--;) \
340
          if ((m)->m_flags & M_EXT) { \
341
                if ((m)->m_ext.ext_free) \
342
                        (*((m)->m_ext.ext_free))(m); \
343
                else \
344
                        MCLFREE((m)->m_ext.ext_buf); \
345
          } \
346
          (n) = (m)->m_next; \
347
          MBUF_FREE((m), mbtypes[(m)->m_type]); \
348
        }
349
 
350
/*
351
 * Copy mbuf pkthdr from from to to.
352
 * from must have M_PKTHDR set, and to must be empty.
353
 */
354
#define M_COPY_PKTHDR(to, from) { \
355
        (to)->m_pkthdr = (from)->m_pkthdr; \
356
        (to)->m_flags = (from)->m_flags & M_COPYFLAGS; \
357
        (to)->m_data = (to)->m_pktdat; \
358
}
359
 
360
/*
361
 * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place
362
 * an object of the specified size at the end of the mbuf, longword aligned.
363
 */
364
#define M_ALIGN(m, len) \
365
        { (m)->m_data += (MLEN - (len)) &~ (sizeof(long) - 1); }
366
/*
367
 * As above, for mbufs allocated with m_gethdr/MGETHDR
368
 * or initialized by M_COPY_PKTHDR.
369
 */
370
#define MH_ALIGN(m, len) \
371
        { (m)->m_data += (MHLEN - (len)) &~ (sizeof(long) - 1); }
372
 
373
/*
374
 * Compute the amount of space available
375
 * before the current start of data in an mbuf.
376
 */
377
#define M_LEADINGSPACE(m) \
378
        ((m)->m_flags & M_EXT ? /* (m)->m_data - (m)->m_ext.ext_buf */ 0 : \
379
            (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat : \
380
            (m)->m_data - (m)->m_dat)
381
 
382
/*
383
 * Compute the amount of space available
384
 * after the end of data in an mbuf.
385
 */
386
#define M_TRAILINGSPACE(m) \
387
        ((m)->m_flags & M_EXT ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size - \
388
            ((m)->m_data + (m)->m_len) : \
389
            &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len))
390
 
391
/*
392
 * Arrange to prepend space of size plen to mbuf m.
393
 * If a new mbuf must be allocated, how specifies whether to wait.
394
 * If how is M_DONTWAIT and allocation fails, the original mbuf chain
395
 * is freed and m is set to NULL.
396
 */
397
#define M_PREPEND(m, plen, how) { \
398
        if (M_LEADINGSPACE(m) >= (plen)) { \
399
                (m)->m_data -= (plen); \
400
                (m)->m_len += (plen); \
401
        } else \
402
                (m) = m_prepend((m), (plen), (how)); \
403
        if ((m) && (m)->m_flags & M_PKTHDR) \
404
                (m)->m_pkthdr.len += (plen); \
405
}
406
 
407
/* change mbuf to new type */
408
#define MCHTYPE(m, t) { \
409
        MBUFLOCK(mbstat.m_mtypes[(m)->m_type]--; mbstat.m_mtypes[t]++;) \
410
        (m)->m_type = t;\
411
}
412
 
413
/* length to m_copy to copy all */
414
#define M_COPYALL       1000000000
415
 
416
/* compatiblity with 4.3 */
417
#define  m_copy(m, o, l)        m_copym((m), (o), (l), M_DONTWAIT)
418
 
419
/*
420
 * Mbuf statistics.
421
 */
422
struct mbstat {
423
        u_long  m_mbufs;        /* mbufs obtained from page pool */
424
        u_long  m_clusters;     /* clusters obtained from page pool */
425
        u_long  m_spare;        /* spare field */
426
        u_long  m_clfree;       /* free clusters */
427
        u_long  m_drops;        /* times failed to find space */
428
        u_long  m_wait;         /* times waited for space */
429
        u_long  m_drain;        /* times drained protocols for space */
430
        u_short m_mtypes[256];  /* type specific mbuf allocations */
431
};
432
 
433
#ifdef  _KERNEL
434
extern  struct mbuf *mbutl;             /* virtual address of mclusters */
435
extern  char *mclrefcnt;                /* cluster reference counts */
436
extern  struct  mbstat mbstat;
437
extern  int nmbclusters;
438
union   mcluster *mclfree;
439
extern  int     max_linkhdr;                    /* largest link-level header */
440
extern  int     max_protohdr;                   /* largest protocol header */
441
extern  int     max_hdr;                        /* largest link+protocol header */
442
extern  int     max_datalen;                    /* MHLEN - max_hdr */
443
extern  int mbtypes[];                  /* XXX */
444
extern  int needqueuedrain;             /* True if allocation failed at */
445
                                        /* interrupt level */
446
 
447
void    mbinit __P((void));
448
struct  mbuf *m_copym2 __P((struct mbuf *, int, int, int));
449
struct  mbuf *m_copym __P((struct mbuf *, int, int, int));
450
struct  mbuf *m_free __P((struct mbuf *));
451
struct  mbuf *m_get __P((int, int));
452
struct  mbuf *m_getclr __P((int, int));
453
struct  mbuf *m_gethdr __P((int, int));
454
struct  mbuf *m_prepend __P((struct mbuf *, int, int));
455
struct  mbuf *m_pullup __P((struct mbuf *, int));
456
struct  mbuf *m_pullup2 __P((struct mbuf *, int));
457
struct  mbuf *m_retry __P((int, int));
458
struct  mbuf *m_retryhdr __P((int, int));
459
struct  mbuf *m_split __P((struct mbuf *, int, int));
460
struct  mbuf *m_inject __P((struct mbuf *, int, int, int));
461
void    m_adj __P((struct mbuf *, int));
462
int     m_clalloc __P((int, int));
463
void    m_copyback __P((struct mbuf *, int, int, caddr_t));
464
void    m_freem __P((struct mbuf *));
465
void    m_reclaim __P((void));
466
void    m_copydata __P((struct mbuf *, int, int, caddr_t));
467
void    m_cat __P((struct mbuf *, struct mbuf *));
468
struct mbuf *m_devget __P((char *, int, int, struct ifnet *,
469
                           void (*) __P((const void *, void *, size_t))));
470
void    m_zero __P((struct mbuf *));
471
int     m_apply __P((struct mbuf *, int, int,
472
                        int (*)(caddr_t, caddr_t, unsigned int), caddr_t));
473
 
474
#endif
475
 
476
#endif // _SYS_MBUF_H_

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.