xref: /original-bsd/sys/sys/mbuf.h (revision 9087ff44)
1 /*
2  * Copyright (c) 1982 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)mbuf.h	6.5 (Berkeley) 06/08/85
7  */
8 
9 /*
10  * Constants related to memory allocator.
11  */
12 #define	MSIZE		128			/* size of an mbuf */
13 #define	MMINOFF		12			/* mbuf header length */
14 #define	MTAIL		4
15 #define	MMAXOFF		(MSIZE-MTAIL)		/* offset where data ends */
16 #define	MLEN		(MSIZE-MMINOFF-MTAIL)	/* mbuf data length */
17 #define	NMBCLUSTERS	256
18 #define	NMBPCL		(CLBYTES/MSIZE)		/* # mbufs per cluster */
19 
20 /*
21  * Macros for type conversion
22  */
23 
24 /* network cluster number to virtual address, and back */
25 #define	cltom(x) ((struct mbuf *)((int)mbutl + ((x) << CLSHIFT)))
26 #define	mtocl(x) (((int)x - (int)mbutl) >> CLSHIFT)
27 
28 /* address in mbuf to mbuf head */
29 #define	dtom(x)		((struct mbuf *)((int)x & ~(MSIZE-1)))
30 
31 /* mbuf head, to typed data */
32 #define	mtod(x,t)	((t)((int)(x) + (x)->m_off))
33 
34 struct mbuf {
35 	struct	mbuf *m_next;		/* next buffer in chain */
36 	u_long	m_off;			/* offset of data */
37 	short	m_len;			/* amount of data in this mbuf */
38 	short	m_type;			/* mbuf type (0 == free) */
39 	u_char	m_dat[MLEN];		/* data storage */
40 	struct	mbuf *m_act;		/* link in higher-level mbuf list */
41 };
42 
43 /* mbuf types */
44 #define	MT_FREE		0	/* should be on free list */
45 #define	MT_DATA		1	/* dynamic (data) allocation */
46 #define	MT_HEADER	2	/* packet header */
47 #define	MT_SOCKET	3	/* socket structure */
48 #define	MT_PCB		4	/* protocol control block */
49 #define	MT_RTABLE	5	/* routing tables */
50 #define	MT_HTABLE	6	/* IMP host tables */
51 #define	MT_ATABLE	7	/* address resolution tables */
52 #define	MT_SONAME	8	/* socket name */
53 #define	MT_ZOMBIE	9	/* zombie proc status */
54 #define	MT_SOOPTS	10	/* socket options */
55 #define	MT_FTABLE	11	/* fragment reassembly header */
56 #define	MT_RIGHTS	12	/* access rights */
57 #define	MT_IFADDR	13	/* interface address */
58 
59 /* flags to m_get */
60 #define	M_DONTWAIT	0
61 #define	M_WAIT		1
62 
63 /* flags to m_pgalloc */
64 #define	MPG_MBUFS	0		/* put new mbufs on free list */
65 #define	MPG_CLUSTERS	1		/* put new clusters on free list */
66 #define	MPG_SPACE	2		/* don't free; caller wants space */
67 
68 /* length to m_copy to copy all */
69 #define	M_COPYALL	1000000000
70 
71 #define	MGET(m, i, t) \
72 	{ int ms = splimp(); \
73 	  if ((m)=mfree) \
74 		{ if ((m)->m_type != MT_FREE) panic("mget"); (m)->m_type = t; \
75 		  mbstat.m_mtypes[MT_FREE]--; mbstat.m_mtypes[t]++; \
76 		  mfree = (m)->m_next; (m)->m_next = 0; \
77 		  (m)->m_off = MMINOFF; } \
78 	  else \
79 		(m) = m_more(i, t); \
80 	  splx(ms); }
81 #define	MCLGET(m, i) \
82 	{ int ms = splimp(); \
83 	  if ((m)=mclfree) \
84 	     {++mclrefcnt[mtocl(m)];mbstat.m_clfree--;mclfree = (m)->m_next;} \
85 	  splx(ms); }
86 #define	MFREE(m, n) \
87 	{ int ms = splimp(); \
88 	  if ((m)->m_type == MT_FREE) panic("mfree"); \
89 	  mbstat.m_mtypes[(m)->m_type]--; mbstat.m_mtypes[MT_FREE]++; \
90 	  (m)->m_type = MT_FREE; \
91 	  if ((m)->m_off > MSIZE) { \
92 		(n) = (struct mbuf *)(mtod(m, int)&~CLOFSET); \
93 		if (--mclrefcnt[mtocl(n)] == 0) \
94 		    { (n)->m_next = mclfree;mclfree = (n);mbstat.m_clfree++;} \
95 	  } \
96 	  (n) = (m)->m_next; (m)->m_next = mfree; \
97 	  (m)->m_off = 0; (m)->m_act = 0; mfree = (m); \
98 	  splx(ms); \
99 	  if (m_want) { \
100 		  m_want = 0; \
101 		  wakeup((caddr_t)mfree); \
102 	  } \
103 	}
104 
105 /*
106  * Mbuf statistics.
107  */
108 struct mbstat {
109 	short	m_mbufs;	/* mbufs obtained from page pool */
110 	short	m_clusters;	/* clusters obtained from page pool */
111 	short	m_clfree;	/* free clusters */
112 	short	m_drops;	/* times failed to find space */
113 	short	m_mtypes[256];	/* type specific mbuf allocations */
114 };
115 
116 #ifdef	KERNEL
117 extern	struct mbuf mbutl[];		/* virtual address of net free mem */
118 extern	struct pte Mbmap[];		/* page tables to map Netutl */
119 struct	mbstat mbstat;
120 int	nmbclusters;
121 struct	mbuf *mfree, *mclfree;
122 char	mclrefcnt[NMBCLUSTERS];
123 int	m_want;
124 struct	mbuf *m_get(),*m_getclr(),*m_free(),*m_more(),*m_copy(),*m_pullup();
125 caddr_t	m_clalloc();
126 #endif
127