source: rtems/cpukit/libnetworking/sys/mbuf.h @ a9dd6de

4.104.114.84.95
Last change on this file since a9dd6de was a9dd6de, checked in by Ralf Corsepius <ralf.corsepius@…>, on 05/09/05 at 13:13:12

Ansification.

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