source: rtems/cpukit/libnetworking/sys/mbuf.h @ 34c63d11

4.104.115
Last change on this file since 34c63d11 was 34c63d11, checked in by Ralf Corsepius <ralf.corsepius@…>, on 04/14/10 at 12:43:21

2010-04-14 Ralf Corsépius <ralf.corsepius@…>

  • libnetworking/sys/mbuf.h: Use uintptr_t instead of u_long for 16bit target compliance.
  • Property mode set to 100644
File size: 13.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. 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)        (((uintptr_t)(x) - (uintptr_t)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        int32_t 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                (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                (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/*
131 * mbuf flags.
132 */
133#define M_EXT           0x0001  /* has associated external storage */
134#define M_PKTHDR        0x0002  /* start of record */
135#define M_EOR           0x0004  /* end of record */
136#define M_PROTO1        0x0008  /* protocol-specific */
137
138/*
139 * mbuf pkthdr flags (also stored in m_flags).
140 */
141#define M_BCAST         0x0100  /* send/received as link-level broadcast */
142#define M_MCAST         0x0200  /* send/received as link-level multicast */
143
144/*
145 * Flags copied when copying m_pkthdr.
146 */
147#define M_COPYFLAGS     (M_PKTHDR|M_EOR|M_PROTO1|M_BCAST|M_MCAST)
148
149/*
150 * mbuf types.
151 */
152#define MT_FREE         0       /* should be on free list */
153#define MT_DATA         1       /* dynamic (data) allocation */
154#define MT_HEADER       2       /* packet header */
155#define MT_SOCKET       3       /* socket structure */
156#define MT_PCB          4       /* protocol control block */
157#define MT_RTABLE       5       /* routing tables */
158#define MT_HTABLE       6       /* IMP host tables */
159#define MT_ATABLE       7       /* address resolution tables */
160#define MT_SONAME       8       /* socket name */
161#define MT_SOOPTS       10      /* socket options */
162#define MT_FTABLE       11      /* fragment reassembly header */
163#define MT_RIGHTS       12      /* access rights */
164#define MT_IFADDR       13      /* interface address */
165#define MT_CONTROL      14      /* extra-data protocol message */
166#define MT_OOBDATA      15      /* expedited data  */
167
168/*
169 * General mbuf allocator statistics structure.
170 */
171struct mbstat {
172        u_long  m_mbufs;        /* mbufs obtained from page pool */
173        u_long  m_clusters;     /* clusters obtained from page pool */
174        u_long  m_spare;        /* spare field */
175        u_long  m_clfree;       /* free clusters */
176        u_long  m_drops;        /* times failed to find space */
177        u_long  m_wait;         /* times waited for space */
178        u_long  m_drain;        /* times drained protocols for space */
179        u_short m_mtypes[256];  /* type specific mbuf allocations */
180};
181
182
183/* flags to m_get/MGET */
184#define M_DONTWAIT      M_NOWAIT
185#define M_WAIT          M_WAITOK
186
187/* Freelists:
188 *
189 * Normal mbuf clusters are normally treated as character arrays
190 * after allocation, but use the first word of the buffer as a free list
191 * pointer while on the free list.
192 */
193union mcluster {
194        union   mcluster *mcl_next;
195        char    mcl_buf[MCLBYTES];
196};
197
198/*
199 * mbuf utility macros:
200 *
201 *      MBUFLOCK(code)
202 * prevents a section of code from from being interrupted by network
203 * drivers.
204 */
205#define MBUFLOCK(code) \
206        { int ms = splimp(); \
207          { code } \
208          splx(ms); \
209        }
210
211/*
212 * mbuf allocation/deallocation macros:
213 *
214 *      MGET(struct mbuf *m, int how, int type)
215 * allocates an mbuf and initializes it to contain internal data.
216 *
217 *      MGETHDR(struct mbuf *m, int how, int type)
218 * allocates an mbuf and initializes it to contain a packet header
219 * and internal data.
220 */
221#define MGET(m, how, type) { \
222          int _ms = splimp(); \
223          if (mmbfree == 0) \
224                (void)m_mballoc(1, (how)); \
225          if (((m) = mmbfree) != 0) { \
226                mmbfree = (m)->m_next; \
227                mbstat.m_mtypes[MT_FREE]--; \
228                (m)->m_type = (type); \
229                mbstat.m_mtypes[type]++; \
230                (m)->m_next = (struct mbuf *)NULL; \
231                (m)->m_nextpkt = (struct mbuf *)NULL; \
232                (m)->m_data = (m)->m_dat; \
233                (m)->m_flags = 0; \
234                splx(_ms); \
235        } else { \
236                splx(_ms); \
237                (m) = m_retry((how), (type)); \
238        } \
239}
240
241#define MGETHDR(m, how, type) { \
242          int _ms = splimp(); \
243          if (mmbfree == 0) \
244                (void)m_mballoc(1, (how)); \
245          if (((m) = mmbfree) != 0) { \
246                mmbfree = (m)->m_next; \
247                mbstat.m_mtypes[MT_FREE]--; \
248                (m)->m_type = (type); \
249                mbstat.m_mtypes[type]++; \
250                (m)->m_next = (struct mbuf *)NULL; \
251                (m)->m_nextpkt = (struct mbuf *)NULL; \
252                (m)->m_data = (m)->m_pktdat; \
253                (m)->m_flags = M_PKTHDR; \
254                splx(_ms); \
255        } else { \
256                splx(_ms); \
257                (m) = m_retryhdr((how), (type)); \
258        } \
259}
260
261/*
262 * Mbuf cluster macros.
263 * MCLALLOC(caddr_t p, int how) allocates an mbuf cluster.
264 * MCLGET adds such clusters to a normal mbuf;
265 * the flag M_EXT is set upon success.
266 * MCLFREE releases a reference to a cluster allocated by MCLALLOC,
267 * freeing the cluster if the reference count has reached 0.
268 */
269#define MCLALLOC(p, how) \
270        MBUFLOCK( \
271          if (mclfree == 0) \
272                (void)m_clalloc(1, (how)); \
273          if (((p) = (caddr_t)mclfree) != 0) { \
274                ++mclrefcnt[mtocl(p)]; \
275                mbstat.m_clfree--; \
276                mclfree = ((union mcluster *)(p))->mcl_next; \
277          } \
278        )
279
280#define MCLGET(m, how) \
281        { MCLALLOC((m)->m_ext.ext_buf, (how)); \
282          if ((m)->m_ext.ext_buf != NULL) { \
283                (m)->m_data = (m)->m_ext.ext_buf; \
284                (m)->m_flags |= M_EXT; \
285                (m)->m_ext.ext_free = NULL;  \
286                (m)->m_ext.ext_ref = NULL;  \
287                (m)->m_ext.ext_size = MCLBYTES;  \
288          } \
289        }
290
291#define MCLFREE(p) \
292        MBUFLOCK ( \
293          if (--mclrefcnt[mtocl(p)] == 0) { \
294                ((union mcluster *)(p))->mcl_next = mclfree; \
295                mclfree = (union mcluster *)(p); \
296                mbstat.m_clfree++; \
297          } \
298        )
299
300/*
301 * MFREE(struct mbuf *m, struct mbuf *n)
302 * Free a single mbuf and associated external storage.
303 * Place the successor, if any, in n.
304 */
305#define MFREE(m, n) \
306        MBUFLOCK(  \
307          mbstat.m_mtypes[(m)->m_type]--; \
308          if ((m)->m_flags & M_EXT) { \
309                if ((m)->m_ext.ext_free) \
310                        (*((m)->m_ext.ext_free))((m)->m_ext.ext_buf, \
311                            (m)->m_ext.ext_size); \
312                else { \
313                        char *p = (m)->m_ext.ext_buf; \
314                        if (--mclrefcnt[mtocl(p)] == 0) { \
315                                ((union mcluster *)(p))->mcl_next = mclfree; \
316                                mclfree = (union mcluster *)(p); \
317                                mbstat.m_clfree++; \
318                        } \
319                } \
320          } \
321          (n) = (m)->m_next; \
322          (m)->m_type = MT_FREE; \
323          mbstat.m_mtypes[MT_FREE]++; \
324          (m)->m_next = mmbfree; \
325          mmbfree = (m); \
326        )
327
328/*
329 * Copy mbuf pkthdr from from to to.
330 * from must have M_PKTHDR set, and to must be empty.
331 */
332#define M_COPY_PKTHDR(to, from) { \
333        (to)->m_pkthdr = (from)->m_pkthdr; \
334        (to)->m_flags = (from)->m_flags & M_COPYFLAGS; \
335        (to)->m_data = (to)->m_pktdat; \
336}
337
338/*
339 * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place
340 * an object of the specified size at the end of the mbuf, longword aligned.
341 */
342#define M_ALIGN(m, len) do {                                            \
343        (m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1);            \
344} while (0)
345
346/*
347 * As above, for mbufs allocated with m_gethdr/MGETHDR
348 * or initialized by M_COPY_PKTHDR.
349 */
350#define MH_ALIGN(m, len) do {                                           \
351        (m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1);           \
352} while (0)
353
354/*
355 * Compute the amount of space available
356 * before the current start of data in an mbuf.
357 */
358#define M_LEADINGSPACE(m) \
359        ((m)->m_flags & M_EXT ? /* (m)->m_data - (m)->m_ext.ext_buf */ 0 : \
360            (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat : \
361            (m)->m_data - (m)->m_dat)
362
363/*
364 * Compute the amount of space available
365 * after the end of data in an mbuf.
366 */
367#define M_TRAILINGSPACE(m) \
368        ((m)->m_flags & M_EXT ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size - \
369            ((m)->m_data + (m)->m_len) : \
370            &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len))
371
372/*
373 * Arrange to prepend space of size plen to mbuf m.
374 * If a new mbuf must be allocated, how specifies whether to wait.
375 * If how is M_DONTWAIT and allocation fails, the original mbuf chain
376 * is freed and m is set to NULL.
377 */
378#define M_PREPEND(m, plen, how) { \
379        if (M_LEADINGSPACE(m) >= (plen)) { \
380                (m)->m_data -= (plen); \
381                (m)->m_len += (plen); \
382        } else \
383                (m) = m_prepend((m), (plen), (how)); \
384        if ((m) && (m)->m_flags & M_PKTHDR) \
385                (m)->m_pkthdr.len += (plen); \
386}
387
388/*
389 * Change mbuf to new type.
390 * This is a relatively expensive operation and should be avoided.
391 */
392#define MCHTYPE(m, t) { \
393        MBUFLOCK(mbstat.m_mtypes[(m)->m_type]--; mbstat.m_mtypes[t]++;) \
394        (m)->m_type = t;\
395}
396
397/* Length to m_copy to copy all. */
398#define M_COPYALL       (uint32_t)1000000000L
399
400/* Compatibility with 4.3. */
401#define  m_copy(m, o, l)        m_copym((m), (o), (l), M_DONTWAIT)
402
403#ifdef  _KERNEL
404extern struct mbuf *mbutl;              /* virtual address of mclusters */
405extern char     *mclrefcnt;             /* cluster reference counts */
406extern struct mbstat mbstat;
407extern uint32_t nmbclusters;
408extern uint32_t nmbufs;
409extern struct mbuf *mmbfree;
410extern union mcluster *mclfree;
411extern int      max_linkhdr;            /* largest link-level header */
412extern int      max_protohdr;           /* largest protocol header */
413extern int      max_hdr;                /* largest link+protocol header */
414extern int      max_datalen;            /* MHLEN - max_hdr */
415
416struct  mbuf *m_copym(struct mbuf *, int, uint32_t, int);
417struct  mbuf *m_copypacket(struct mbuf *, int);
418struct  mbuf *m_devget(char *, int, int, struct ifnet *,
419                            void (*copy)(char *, caddr_t, u_int));
420struct  mbuf *m_free(struct mbuf *);
421struct  mbuf *m_get(int, int);
422struct  mbuf *m_getclr(int, int);
423struct  mbuf *m_gethdr(int, int);
424struct  mbuf *m_prepend(struct mbuf *,int,int);
425struct  mbuf *m_pullup(struct mbuf *, int);
426struct  mbuf *m_retry(int, int);
427struct  mbuf *m_retryhdr(int, int);
428struct  mbuf *m_split(struct mbuf *,int,int);
429void    m_adj(struct mbuf *, int);
430void    m_cat(struct mbuf *,struct mbuf *);
431int     m_mballoc(int, int);
432int     m_clalloc(int, int);
433int     m_copyback(struct mbuf *, int, int, caddr_t);
434int     m_copydata(const struct mbuf *, int, int, caddr_t);
435void    m_freem(struct mbuf *);
436void    m_reclaim(void);
437
438#endif /* _KERNEL */
439
440#endif /* !_SYS_MBUF_H_ */
Note: See TracBrowser for help on using the repository browser.