source: rtems/c/src/lib/libnetworking/sys/mbuf.h @ 354b00bc

4.104.114.84.95
Last change on this file since 354b00bc was 354b00bc, checked in by Joel Sherrill <joel.sherrill@…>, on 02/04/99 at 15:00:14

Added printf()'s that can be uncommented to trace MBUF operations. This
is very useful when debugging a device driver.

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