]>
Commit | Line | Data |
---|---|---|
f0cbd3ec FB |
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 | ||
0fe6a7f2 | 20 | static int mbuf_alloced; |
f0cbd3ec | 21 | struct mbuf m_freelist, m_usedlist; |
9634d903 | 22 | #define MBUF_THRESH 30 |
9634d903 BS |
23 | |
24 | /* | |
25 | * Find a nice value for msize | |
26 | * XXX if_maxlinkhdr already in mtu | |
27 | */ | |
79383c9c | 28 | #define SLIRP_MSIZE (IF_MTU + IF_MAXLINKHDR + sizeof(struct m_hdr ) + 6) |
f0cbd3ec FB |
29 | |
30 | void | |
511d2b14 | 31 | m_init(void) |
f0cbd3ec FB |
32 | { |
33 | m_freelist.m_next = m_freelist.m_prev = &m_freelist; | |
34 | m_usedlist.m_next = m_usedlist.m_prev = &m_usedlist; | |
f0cbd3ec FB |
35 | } |
36 | ||
37 | /* | |
38 | * Get an mbuf from the free list, if there are none | |
39 | * malloc one | |
5fafdf24 | 40 | * |
f0cbd3ec FB |
41 | * Because fragmentation can occur if we alloc new mbufs and |
42 | * free old mbufs, we mark all mbufs above mbuf_thresh as M_DOFREE, | |
43 | * which tells m_free to actually free() it | |
44 | */ | |
45 | struct mbuf * | |
511d2b14 | 46 | m_get(void) |
f0cbd3ec FB |
47 | { |
48 | register struct mbuf *m; | |
49 | int flags = 0; | |
5fafdf24 | 50 | |
f0cbd3ec | 51 | DEBUG_CALL("m_get"); |
5fafdf24 | 52 | |
f0cbd3ec | 53 | if (m_freelist.m_next == &m_freelist) { |
79383c9c | 54 | m = (struct mbuf *)malloc(SLIRP_MSIZE); |
f0cbd3ec FB |
55 | if (m == NULL) goto end_error; |
56 | mbuf_alloced++; | |
9634d903 | 57 | if (mbuf_alloced > MBUF_THRESH) |
f0cbd3ec | 58 | flags = M_DOFREE; |
f0cbd3ec FB |
59 | } else { |
60 | m = m_freelist.m_next; | |
61 | remque(m); | |
62 | } | |
5fafdf24 | 63 | |
f0cbd3ec FB |
64 | /* Insert it in the used list */ |
65 | insque(m,&m_usedlist); | |
66 | m->m_flags = (flags | M_USEDLIST); | |
5fafdf24 | 67 | |
f0cbd3ec | 68 | /* Initialise it */ |
79383c9c | 69 | m->m_size = SLIRP_MSIZE - sizeof(struct m_hdr); |
f0cbd3ec FB |
70 | m->m_data = m->m_dat; |
71 | m->m_len = 0; | |
511d2b14 BS |
72 | m->m_nextpkt = NULL; |
73 | m->m_prevpkt = NULL; | |
f0cbd3ec FB |
74 | end_error: |
75 | DEBUG_ARG("m = %lx", (long )m); | |
76 | return m; | |
77 | } | |
78 | ||
79 | void | |
511d2b14 | 80 | m_free(struct mbuf *m) |
f0cbd3ec | 81 | { |
5fafdf24 | 82 | |
f0cbd3ec FB |
83 | DEBUG_CALL("m_free"); |
84 | DEBUG_ARG("m = %lx", (long )m); | |
5fafdf24 | 85 | |
f0cbd3ec FB |
86 | if(m) { |
87 | /* Remove from m_usedlist */ | |
88 | if (m->m_flags & M_USEDLIST) | |
89 | remque(m); | |
5fafdf24 | 90 | |
f0cbd3ec FB |
91 | /* If it's M_EXT, free() it */ |
92 | if (m->m_flags & M_EXT) | |
93 | free(m->m_ext); | |
94 | ||
95 | /* | |
96 | * Either free() it or put it on the free list | |
97 | */ | |
98 | if (m->m_flags & M_DOFREE) { | |
99 | free(m); | |
100 | mbuf_alloced--; | |
101 | } else if ((m->m_flags & M_FREELIST) == 0) { | |
102 | insque(m,&m_freelist); | |
103 | m->m_flags = M_FREELIST; /* Clobber other flags */ | |
104 | } | |
105 | } /* if(m) */ | |
106 | } | |
107 | ||
108 | /* | |
109 | * Copy data from one mbuf to the end of | |
110 | * the other.. if result is too big for one mbuf, malloc() | |
111 | * an M_EXT data segment | |
112 | */ | |
113 | void | |
511d2b14 | 114 | m_cat(struct mbuf *m, struct mbuf *n) |
f0cbd3ec FB |
115 | { |
116 | /* | |
117 | * If there's no room, realloc | |
118 | */ | |
119 | if (M_FREEROOM(m) < n->m_len) | |
120 | m_inc(m,m->m_size+MINCSIZE); | |
5fafdf24 | 121 | |
f0cbd3ec FB |
122 | memcpy(m->m_data+m->m_len, n->m_data, n->m_len); |
123 | m->m_len += n->m_len; | |
124 | ||
125 | m_free(n); | |
126 | } | |
127 | ||
128 | ||
129 | /* make m size bytes large */ | |
130 | void | |
511d2b14 | 131 | m_inc(struct mbuf *m, int size) |
f0cbd3ec | 132 | { |
4b6ccfde FB |
133 | int datasize; |
134 | ||
f0cbd3ec FB |
135 | /* some compiles throw up on gotos. This one we can fake. */ |
136 | if(m->m_size>size) return; | |
137 | ||
138 | if (m->m_flags & M_EXT) { | |
4b6ccfde | 139 | datasize = m->m_data - m->m_ext; |
f0cbd3ec | 140 | m->m_ext = (char *)realloc(m->m_ext,size); |
4b6ccfde | 141 | m->m_data = m->m_ext + datasize; |
f0cbd3ec | 142 | } else { |
f0cbd3ec FB |
143 | char *dat; |
144 | datasize = m->m_data - m->m_dat; | |
145 | dat = (char *)malloc(size); | |
f0cbd3ec | 146 | memcpy(dat, m->m_dat, m->m_size); |
3b46e624 | 147 | |
f0cbd3ec FB |
148 | m->m_ext = dat; |
149 | m->m_data = m->m_ext + datasize; | |
150 | m->m_flags |= M_EXT; | |
151 | } | |
5fafdf24 | 152 | |
f0cbd3ec FB |
153 | m->m_size = size; |
154 | ||
155 | } | |
156 | ||
157 | ||
158 | ||
159 | void | |
511d2b14 | 160 | m_adj(struct mbuf *m, int len) |
f0cbd3ec FB |
161 | { |
162 | if (m == NULL) | |
163 | return; | |
164 | if (len >= 0) { | |
165 | /* Trim from head */ | |
166 | m->m_data += len; | |
167 | m->m_len -= len; | |
168 | } else { | |
169 | /* Trim from tail */ | |
170 | len = -len; | |
171 | m->m_len -= len; | |
172 | } | |
173 | } | |
174 | ||
175 | ||
176 | /* | |
177 | * Copy len bytes from m, starting off bytes into n | |
178 | */ | |
179 | int | |
511d2b14 | 180 | m_copy(struct mbuf *n, struct mbuf *m, int off, int len) |
f0cbd3ec FB |
181 | { |
182 | if (len > M_FREEROOM(n)) | |
183 | return -1; | |
184 | ||
185 | memcpy((n->m_data + n->m_len), (m->m_data + off), len); | |
186 | n->m_len += len; | |
187 | return 0; | |
188 | } | |
189 | ||
190 | ||
191 | /* | |
192 | * Given a pointer into an mbuf, return the mbuf | |
193 | * XXX This is a kludge, I should eliminate the need for it | |
194 | * Fortunately, it's not used often | |
195 | */ | |
196 | struct mbuf * | |
511d2b14 | 197 | dtom(void *dat) |
f0cbd3ec FB |
198 | { |
199 | struct mbuf *m; | |
5fafdf24 | 200 | |
f0cbd3ec FB |
201 | DEBUG_CALL("dtom"); |
202 | DEBUG_ARG("dat = %lx", (long )dat); | |
203 | ||
204 | /* bug corrected for M_EXT buffers */ | |
205 | for (m = m_usedlist.m_next; m != &m_usedlist; m = m->m_next) { | |
206 | if (m->m_flags & M_EXT) { | |
207 | if( (char *)dat>=m->m_ext && (char *)dat<(m->m_ext + m->m_size) ) | |
208 | return m; | |
209 | } else { | |
210 | if( (char *)dat >= m->m_dat && (char *)dat<(m->m_dat + m->m_size) ) | |
211 | return m; | |
212 | } | |
213 | } | |
5fafdf24 | 214 | |
f0cbd3ec | 215 | DEBUG_ERROR((dfd, "dtom failed")); |
5fafdf24 | 216 | |
f0cbd3ec FB |
217 | return (struct mbuf *)0; |
218 | } |