source: rtems/cpukit/libnetworking/sys/mbuf.h @ 9e7678d

4.104.114.84.95
Last change on this file since 9e7678d was a3c06e11, checked in by Ralf Corsepius <ralf.corsepius@…>, on 04/18/05 at 05:20:02

Cosmetics from FreeBSD.

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