1: /*
2: * Copyright (c) 1982, 1986 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 7.1 (Berkeley) 6/4/86
7: */
8:
9: /*
10: * Constants related to memory allocator.
11: */
12: #define MSIZE 128 /* size of an mbuf */
13:
14: #define MMINOFF 12 /* mbuf header length */
15: #define MTAIL 4
16: #define MMAXOFF (MSIZE-MTAIL) /* offset where data ends */
17: #define MLEN (MSIZE-MMINOFF-MTAIL) /* mbuf data length */
18: #define NMBCLUSTERS 256
19: #define NMBPCL (CLBYTES/MSIZE) /* # mbufs per cluster */
20:
21: /*
22: * Macros for type conversion
23: */
24:
25: /* network cluster number to virtual address, and back */
26: #define cltom(x) ((struct mbuf *)((int)mbutl + ((x) << CLSHIFT)))
27: #define mtocl(x) (((int)x - (int)mbutl) >> CLSHIFT)
28:
29: /* address in mbuf to mbuf head */
30: #define dtom(x) ((struct mbuf *)((int)x & ~(MSIZE-1)))
31:
32: /* mbuf head, to typed data */
33: #define mtod(x,t) ((t)((int)(x) + (x)->m_off))
34:
35: struct mbuf {
36: struct mbuf *m_next; /* next buffer in chain */
37: u_long m_off; /* offset of data */
38: short m_len; /* amount of data in this mbuf */
39: short m_type; /* mbuf type (0 == free) */
40: u_char m_dat[MLEN]; /* data storage */
41: struct mbuf *m_act; /* link in higher-level mbuf list */
42: };
43:
44: /* mbuf types */
45: #define MT_FREE 0 /* should be on free list */
46: #define MT_DATA 1 /* dynamic (data) allocation */
47: #define 2 /* packet header */
48: #define MT_SOCKET 3 /* socket structure */
49: #define MT_PCB 4 /* protocol control block */
50: #define MT_RTABLE 5 /* routing tables */
51: #define MT_HTABLE 6 /* IMP host tables */
52: #define MT_ATABLE 7 /* address resolution tables */
53: #define MT_SONAME 8 /* socket name */
54: #define MT_ZOMBIE 9 /* zombie proc status */
55: #define MT_SOOPTS 10 /* socket options */
56: #define MT_FTABLE 11 /* fragment reassembly header */
57: #define MT_RIGHTS 12 /* access rights */
58: #define MT_IFADDR 13 /* interface address */
59:
60: /* flags to m_get */
61: #define M_DONTWAIT 0
62: #define M_WAIT 1
63:
64: /* flags to m_pgalloc */
65: #define MPG_MBUFS 0 /* put new mbufs on free list */
66: #define MPG_CLUSTERS 1 /* put new clusters on free list */
67: #define MPG_SPACE 2 /* don't free; caller wants space */
68:
69: /* length to m_copy to copy all */
70: #define M_COPYALL 1000000000
71:
72: /*
73: * m_pullup will pull up additional length if convenient;
74: * should be enough to hold headers of second-level and higher protocols.
75: */
76: #define 32
77:
78: #define MGET(m, i, t) \
79: { int ms = splimp(); \
80: if ((m)=mfree) \
81: { if ((m)->m_type != MT_FREE) panic("mget"); (m)->m_type = t; \
82: mbstat.m_mtypes[MT_FREE]--; mbstat.m_mtypes[t]++; \
83: mfree = (m)->m_next; (m)->m_next = 0; \
84: (m)->m_off = MMINOFF; } \
85: else \
86: (m) = m_more(i, t); \
87: splx(ms); }
88: /*
89: * Mbuf page cluster macros.
90: * MCLALLOC allocates mbuf page clusters.
91: * Note that it works only with a count of 1 at the moment.
92: * MCLGET adds such clusters to a normal mbuf.
93: * m->m_len is set to CLBYTES upon success.
94: * MCLFREE frees clusters allocated by MCLALLOC.
95: */
96: #define MCLALLOC(m, i) \
97: { int ms = splimp(); \
98: if ((m)=mclfree) \
99: {++mclrefcnt[mtocl(m)];mbstat.m_clfree--;mclfree = (m)->m_next;} \
100: splx(ms); }
101: #define M_HASCL(m) ((m)->m_off >= MSIZE)
102: #define MTOCL(m) ((struct mbuf *)(mtod((m), int)&~CLOFSET))
103:
104: #define MCLGET(m) \
105: { struct mbuf *p; \
106: if (mclfree == 0) \
107: (void)m_clalloc(1, MPG_CLUSTERS, M_DONTWAIT); \
108: MCLALLOC(p, 1); \
109: if (p) { \
110: (m)->m_off = (int)p - (int)(m); \
111: (m)->m_len = CLBYTES; \
112: } \
113: }
114: #define MCLFREE(m) { \
115: if (--mclrefcnt[mtocl(m)] == 0) \
116: { (m)->m_next = mclfree;mclfree = (m);mbstat.m_clfree++;} \
117: }
118: #define MFREE(m, n) \
119: { int ms = splimp(); \
120: if ((m)->m_type == MT_FREE) panic("mfree"); \
121: mbstat.m_mtypes[(m)->m_type]--; mbstat.m_mtypes[MT_FREE]++; \
122: (m)->m_type = MT_FREE; \
123: if (M_HASCL(m)) { \
124: (n) = MTOCL(m); \
125: MCLFREE(n); \
126: } \
127: (n) = (m)->m_next; (m)->m_next = mfree; \
128: (m)->m_off = 0; (m)->m_act = 0; mfree = (m); \
129: splx(ms); \
130: if (m_want) { \
131: m_want = 0; \
132: wakeup((caddr_t)&mfree); \
133: } \
134: }
135:
136: /*
137: * Mbuf statistics.
138: */
139: struct mbstat {
140: short m_mbufs; /* mbufs obtained from page pool */
141: short m_clusters; /* clusters obtained from page pool */
142: short m_clfree; /* free clusters */
143: short m_drops; /* times failed to find space */
144: short m_mtypes[256]; /* type specific mbuf allocations */
145: };
146:
147: #ifdef KERNEL
148: extern struct mbuf mbutl[]; /* virtual address of net free mem */
149: extern struct pte Mbmap[]; /* page tables to map Netutl */
150: struct mbstat mbstat;
151: int nmbclusters;
152: struct mbuf *mfree, *mclfree;
153: char mclrefcnt[NMBCLUSTERS + 1];
154: int m_want;
155: struct mbuf *m_get(),*m_getclr(),*m_free(),*m_more(),*m_copy(),*m_pullup();
156: caddr_t m_clalloc();
157: #endif