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