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

5
Last change on this file since eddb134 was eddb134, checked in by Sebastian Huber <sebastian.huber@…>, on 05/22/17 at 10:38:32

network: Do not use MSIZE

Do not use the MSIZE for the legacy network stack. Instead use
_SYS_MBUF_LEGACY_MSIZE.

Update #2833.

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