1 | /*
|
---|
2 | * Copyright (c) 1995 Danny Gasparovski
|
---|
3 | *
|
---|
4 | * Please read the file COPYRIGHT for the
|
---|
5 | * terms and conditions of the copyright.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * mbuf's in SLiRP are much simpler than the real mbufs in
|
---|
10 | * FreeBSD. They are fixed size, determined by the MTU,
|
---|
11 | * so that one whole packet can fit. Mbuf's cannot be
|
---|
12 | * chained together. If there's more data than the mbuf
|
---|
13 | * could hold, an external malloced buffer is pointed to
|
---|
14 | * by m_ext (and the data pointers) and M_EXT is set in
|
---|
15 | * the flags
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include <slirp.h>
|
---|
19 |
|
---|
20 |
|
---|
21 | void
|
---|
22 | m_init(PNATState pData)
|
---|
23 | {
|
---|
24 | m_freelist.m_next = m_freelist.m_prev = &m_freelist;
|
---|
25 | m_usedlist.m_next = m_usedlist.m_prev = &m_usedlist;
|
---|
26 | mbuf_alloced = 0;
|
---|
27 | msize_init(pData);
|
---|
28 | }
|
---|
29 |
|
---|
30 | void
|
---|
31 | msize_init(PNATState pData)
|
---|
32 | {
|
---|
33 | /*
|
---|
34 | * Find a nice value for msize
|
---|
35 | * XXX if_maxlinkhdr already in mtu
|
---|
36 | */
|
---|
37 | msize = (if_mtu>if_mru ? if_mtu : if_mru)
|
---|
38 | + if_maxlinkhdr + sizeof(struct m_hdr ) + 6;
|
---|
39 | }
|
---|
40 |
|
---|
41 | /*
|
---|
42 | * Get an mbuf from the free list, if there are none
|
---|
43 | * malloc one
|
---|
44 | *
|
---|
45 | * Because fragmentation can occur if we alloc new mbufs and
|
---|
46 | * free old mbufs, we mark all mbufs above mbuf_thresh as M_DOFREE,
|
---|
47 | * which tells m_free to actually free() it
|
---|
48 | */
|
---|
49 | struct mbuf *
|
---|
50 | m_get(PNATState pData)
|
---|
51 | {
|
---|
52 | register struct mbuf *m;
|
---|
53 | int flags = 0;
|
---|
54 |
|
---|
55 | DEBUG_CALL("m_get");
|
---|
56 |
|
---|
57 | if (m_freelist.m_next == &m_freelist)
|
---|
58 | {
|
---|
59 | m = (struct mbuf *)RTMemAlloc(msize);
|
---|
60 | if (m == NULL)
|
---|
61 | goto end_error;
|
---|
62 | mbuf_alloced++;
|
---|
63 | if (mbuf_alloced > mbuf_thresh)
|
---|
64 | flags = M_DOFREE;
|
---|
65 | if (mbuf_alloced > mbuf_max)
|
---|
66 | mbuf_max = mbuf_alloced;
|
---|
67 | }
|
---|
68 | else
|
---|
69 | {
|
---|
70 | m = m_freelist.m_next;
|
---|
71 | remque(pData, m);
|
---|
72 | }
|
---|
73 |
|
---|
74 | /* Insert it in the used list */
|
---|
75 | insque(pData, m, &m_usedlist);
|
---|
76 | m->m_flags = (flags | M_USEDLIST);
|
---|
77 |
|
---|
78 | /* Initialise it */
|
---|
79 | m->m_size = msize - sizeof(struct m_hdr);
|
---|
80 | m->m_data = m->m_dat;
|
---|
81 | m->m_len = 0;
|
---|
82 | m->m_nextpkt = 0;
|
---|
83 | m->m_prevpkt = 0;
|
---|
84 |
|
---|
85 | end_error:
|
---|
86 | DEBUG_ARG("m = %lx", (long )m);
|
---|
87 | return m;
|
---|
88 | }
|
---|
89 |
|
---|
90 | void
|
---|
91 | m_free(PNATState pData, struct mbuf *m)
|
---|
92 | {
|
---|
93 |
|
---|
94 | DEBUG_CALL("m_free");
|
---|
95 | DEBUG_ARG("m = %lx", (long )m);
|
---|
96 |
|
---|
97 | if(m)
|
---|
98 | {
|
---|
99 | /* Remove from m_usedlist */
|
---|
100 | if (m->m_flags & M_USEDLIST)
|
---|
101 | remque(pData, m);
|
---|
102 |
|
---|
103 | /* If it's M_EXT, free() it */
|
---|
104 | if (m->m_flags & M_EXT)
|
---|
105 | RTMemFree(m->m_ext);
|
---|
106 |
|
---|
107 | /*
|
---|
108 | * Either free() it or put it on the free list
|
---|
109 | */
|
---|
110 | if (m->m_flags & M_DOFREE)
|
---|
111 | {
|
---|
112 | RTMemFree(m);
|
---|
113 | mbuf_alloced--;
|
---|
114 | }
|
---|
115 | else if ((m->m_flags & M_FREELIST) == 0)
|
---|
116 | {
|
---|
117 | insque(pData, m,&m_freelist);
|
---|
118 | m->m_flags = M_FREELIST; /* Clobber other flags */
|
---|
119 | }
|
---|
120 | } /* if(m) */
|
---|
121 | }
|
---|
122 |
|
---|
123 | /*
|
---|
124 | * Copy data from one mbuf to the end of
|
---|
125 | * the other.. if result is too big for one mbuf, malloc()
|
---|
126 | * an M_EXT data segment
|
---|
127 | */
|
---|
128 | void
|
---|
129 | m_cat(PNATState pData, register struct mbuf *m, register struct mbuf *n)
|
---|
130 | {
|
---|
131 | /*
|
---|
132 | * If there's no room, realloc
|
---|
133 | */
|
---|
134 | if (M_FREEROOM(m) < n->m_len)
|
---|
135 | m_inc(m,m->m_size+MINCSIZE);
|
---|
136 |
|
---|
137 | memcpy(m->m_data+m->m_len, n->m_data, n->m_len);
|
---|
138 | m->m_len += n->m_len;
|
---|
139 |
|
---|
140 | m_free(pData, n);
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | /* make m size bytes large */
|
---|
145 | void
|
---|
146 | m_inc(struct mbuf *m, int size)
|
---|
147 | {
|
---|
148 | int datasize;
|
---|
149 |
|
---|
150 | /* some compiles throw up on gotos. This one we can fake. */
|
---|
151 | if (m->m_size > size)
|
---|
152 | return;
|
---|
153 |
|
---|
154 | if (m->m_flags & M_EXT)
|
---|
155 | {
|
---|
156 | datasize = m->m_data - m->m_ext;
|
---|
157 | m->m_ext = (char *)RTMemRealloc(m->m_ext, size);
|
---|
158 | #if 0
|
---|
159 | if (m->m_ext == NULL)
|
---|
160 | return (struct mbuf *)NULL;
|
---|
161 | #endif
|
---|
162 | m->m_data = m->m_ext + datasize;
|
---|
163 | }
|
---|
164 | else
|
---|
165 | {
|
---|
166 | char *dat;
|
---|
167 | datasize = m->m_data - m->m_dat;
|
---|
168 | dat = (char *)RTMemAlloc(size);
|
---|
169 | #if 0
|
---|
170 | if (dat == NULL)
|
---|
171 | return (struct mbuf *)NULL;
|
---|
172 | #endif
|
---|
173 | memcpy(dat, m->m_dat, m->m_size);
|
---|
174 |
|
---|
175 | m->m_ext = dat;
|
---|
176 | m->m_data = m->m_ext + datasize;
|
---|
177 | m->m_flags |= M_EXT;
|
---|
178 | }
|
---|
179 |
|
---|
180 | m->m_size = size;
|
---|
181 | }
|
---|
182 |
|
---|
183 |
|
---|
184 | void
|
---|
185 | m_adj(struct mbuf *m, int len)
|
---|
186 | {
|
---|
187 | if (m == NULL)
|
---|
188 | return;
|
---|
189 | if (len >= 0)
|
---|
190 | {
|
---|
191 | /* Trim from head */
|
---|
192 | m->m_data += len;
|
---|
193 | m->m_len -= len;
|
---|
194 | }
|
---|
195 | else
|
---|
196 | {
|
---|
197 | /* Trim from tail */
|
---|
198 | len = -len;
|
---|
199 | m->m_len -= len;
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | /*
|
---|
205 | * Copy len bytes from m, starting off bytes into n
|
---|
206 | */
|
---|
207 | int
|
---|
208 | m_copy(struct mbuf *n, struct mbuf *m, int off, int len)
|
---|
209 | {
|
---|
210 | if (len > M_FREEROOM(n))
|
---|
211 | return -1;
|
---|
212 |
|
---|
213 | memcpy((n->m_data + n->m_len), (m->m_data + off), len);
|
---|
214 | n->m_len += len;
|
---|
215 | return 0;
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | /*
|
---|
220 | * Given a pointer into an mbuf, return the mbuf
|
---|
221 | * XXX This is a kludge, I should eliminate the need for it
|
---|
222 | * Fortunately, it's not used often
|
---|
223 | */
|
---|
224 | struct mbuf *
|
---|
225 | dtom(PNATState pData, void *dat)
|
---|
226 | {
|
---|
227 | struct mbuf *m;
|
---|
228 |
|
---|
229 | DEBUG_CALL("dtom");
|
---|
230 | DEBUG_ARG("dat = %lx", (long )dat);
|
---|
231 |
|
---|
232 | /* bug corrected for M_EXT buffers */
|
---|
233 | for (m = m_usedlist.m_next; m != &m_usedlist; m = m->m_next)
|
---|
234 | {
|
---|
235 | if (m->m_flags & M_EXT)
|
---|
236 | {
|
---|
237 | if ( (char *)dat >= m->m_ext
|
---|
238 | && (char *)dat < (m->m_ext + m->m_size))
|
---|
239 | return m;
|
---|
240 | }
|
---|
241 | else
|
---|
242 | {
|
---|
243 | if ( (char *)dat >= m->m_dat
|
---|
244 | && (char *)dat < (m->m_dat + m->m_size))
|
---|
245 | return m;
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | DEBUG_ERROR((dfd, "dtom failed"));
|
---|
250 |
|
---|
251 | return (struct mbuf *)0;
|
---|
252 | }
|
---|