FreeBSD/Linux Kernel Cross Reference
sys/kern/uipc_mbuf2.c
1 /* $FreeBSD: src/sys/kern/uipc_mbuf2.c,v 1.2.2.5 2003/01/23 21:06:44 sam Exp $ */
2 /* $DragonFly: src/sys/kern/uipc_mbuf2.c,v 1.15 2007/10/20 10:28:44 sephe Exp $ */
3 /* $KAME: uipc_mbuf2.c,v 1.31 2001/11/28 11:08:53 itojun Exp $ */
4 /* $NetBSD: uipc_mbuf.c,v 1.40 1999/04/01 00:23:25 thorpej Exp $ */
5
6 /*
7 * Copyright (C) 1999 WIDE Project.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the project nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 /*
36 * Copyright (c) 1982, 1986, 1988, 1991, 1993
37 * The Regents of the University of California. All rights reserved.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 *
63 * @(#)uipc_mbuf.c 8.4 (Berkeley) 2/14/95
64 */
65
66 /*#define PULLDOWN_DEBUG*/
67
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/kernel.h>
71 #include <sys/proc.h>
72 #include <sys/malloc.h>
73 #include <sys/mbuf.h>
74
75 MALLOC_DEFINE(M_PACKET_TAGS, "tag", "packet-attached information");
76
77 /* can't call it m_dup(), as freebsd[34] uses m_dup() with different arg */
78 static struct mbuf *m_dup1 (struct mbuf *, int, int, int);
79
80 /*
81 * ensure that [off, off + len) is contiguous on the mbuf chain "m".
82 * packet chain before "off" is kept untouched.
83 * if offp == NULL, the target will start at <retval, 0> on resulting chain.
84 * if offp != NULL, the target will start at <retval, *offp> on resulting chain.
85 *
86 * on error return (NULL return value), original "m" will be freed.
87 *
88 * XXX M_TRAILINGSPACE/M_LEADINGSPACE on shared cluster (sharedcluster)
89 */
90 struct mbuf *
91 m_pulldown(struct mbuf *m, int off, int len, int *offp)
92 {
93 struct mbuf *n, *o;
94 int hlen, tlen, olen;
95 int sharedcluster;
96
97 /* check invalid arguments. */
98 if (m == NULL)
99 panic("m == NULL in m_pulldown()");
100 if (len > MCLBYTES) {
101 m_freem(m);
102 return NULL; /* impossible */
103 }
104
105 #ifdef PULLDOWN_DEBUG
106 {
107 struct mbuf *t;
108 kprintf("before:");
109 for (t = m; t; t = t->m_next)
110 kprintf(" %d", t->m_len);
111 kprintf("\n");
112 }
113 #endif
114 n = m;
115 while (n != NULL && off > 0) {
116 if (n->m_len > off)
117 break;
118 off -= n->m_len;
119 n = n->m_next;
120 }
121 /* be sure to point non-empty mbuf */
122 while (n != NULL && n->m_len == 0)
123 n = n->m_next;
124 if (!n) {
125 m_freem(m);
126 return NULL; /* mbuf chain too short */
127 }
128
129 sharedcluster = (m_sharecount(n) > 1);
130
131 /*
132 * the target data is on <n, off>.
133 * if we got enough data on the mbuf "n", we're done.
134 */
135 if ((off == 0 || offp) && len <= n->m_len - off && !sharedcluster)
136 goto ok;
137
138 /*
139 * when len <= n->m_len - off and off != 0, it is a special case.
140 * len bytes from <n, off> sits in single mbuf, but the caller does
141 * not like the starting position (off).
142 * chop the current mbuf into two pieces, set off to 0.
143 */
144 if (len <= n->m_len - off) {
145 o = m_dup1(n, off, n->m_len - off, MB_DONTWAIT);
146 if (o == NULL) {
147 m_freem(m);
148 return NULL; /* ENOBUFS */
149 }
150 n->m_len = off;
151 o->m_next = n->m_next;
152 n->m_next = o;
153 n = n->m_next;
154 off = 0;
155 goto ok;
156 }
157
158 /*
159 * we need to take hlen from <n, off> and tlen from <n->m_next, 0>,
160 * and construct contiguous mbuf with m_len == len.
161 * note that hlen + tlen == len, and tlen > 0.
162 */
163 hlen = n->m_len - off;
164 tlen = len - hlen;
165
166 /*
167 * ensure that we have enough trailing data on mbuf chain.
168 * if not, we can do nothing about the chain.
169 */
170 olen = 0;
171 for (o = n->m_next; o != NULL; o = o->m_next)
172 olen += o->m_len;
173 if (hlen + olen < len) {
174 m_freem(m);
175 return NULL; /* mbuf chain too short */
176 }
177
178 /*
179 * easy cases first.
180 * we need to use m_copydata() to get data from <n->m_next, 0>.
181 */
182 if ((off == 0 || offp) && M_TRAILINGSPACE(n) >= tlen &&
183 !sharedcluster) {
184 m_copydata(n->m_next, 0, tlen, mtod(n, caddr_t) + n->m_len);
185 n->m_len += tlen;
186 m_adj(n->m_next, tlen);
187 goto ok;
188 }
189 if ((off == 0 || offp) && M_LEADINGSPACE(n->m_next) >= hlen &&
190 !sharedcluster) {
191 n->m_next->m_data -= hlen;
192 n->m_next->m_len += hlen;
193 bcopy(mtod(n, caddr_t) + off, mtod(n->m_next, caddr_t), hlen);
194 n->m_len -= hlen;
195 n = n->m_next;
196 off = 0;
197 goto ok;
198 }
199
200 /*
201 * now, we need to do the hard way. don't m_copy as there's no room
202 * on both end.
203 */
204 o = m_getl(len, MB_DONTWAIT, m->m_type, 0, NULL);
205 if (!o) {
206 m_freem(m);
207 return NULL; /* ENOBUFS */
208 }
209 /* get hlen from <n, off> into <o, 0> */
210 o->m_len = hlen;
211 bcopy(mtod(n, caddr_t) + off, mtod(o, caddr_t), hlen);
212 n->m_len -= hlen;
213 /* get tlen from <n->m_next, 0> into <o, hlen> */
214 m_copydata(n->m_next, 0, tlen, mtod(o, caddr_t) + o->m_len);
215 o->m_len += tlen;
216 m_adj(n->m_next, tlen);
217 o->m_next = n->m_next;
218 n->m_next = o;
219 n = o;
220 off = 0;
221
222 ok:
223 #ifdef PULLDOWN_DEBUG
224 {
225 struct mbuf *t;
226 kprintf("after:");
227 for (t = m; t; t = t->m_next)
228 kprintf("%c%d", t == n ? '*' : ' ', t->m_len);
229 kprintf(" (off=%d)\n", off);
230 }
231 #endif
232 if (offp)
233 *offp = off;
234 return n;
235 }
236
237 static struct mbuf *
238 m_dup1(struct mbuf *m, int off, int len, int wait)
239 {
240 struct mbuf *n;
241
242 if (len > MCLBYTES)
243 return NULL;
244 n = m_getl(len, wait, m->m_type, m->m_flags, NULL);
245 if (!n)
246 return NULL;
247 if (off == 0 && (m->m_flags & M_PKTHDR) && !m_dup_pkthdr(n, m, wait)) {
248 m_free(n);
249 return NULL;
250 }
251 m_copydata(m, off, len, mtod(n, caddr_t));
252 return n;
253 }
254
255 /* Get a packet tag structure along with specified data following. */
256 struct m_tag *
257 m_tag_alloc(uint32_t cookie, int type, int len, int wait)
258 {
259 struct m_tag *t;
260
261 if (len < 0)
262 return NULL;
263 t = kmalloc(len + sizeof(struct m_tag), M_PACKET_TAGS, MBTOM(wait));
264 if (t == NULL)
265 return NULL;
266 t->m_tag_id = type;
267 t->m_tag_len = len;
268 t->m_tag_cookie = cookie;
269 return t;
270 }
271
272
273 /* Free a packet tag. */
274 void
275 m_tag_free(struct m_tag *t)
276 {
277 kfree(t, M_PACKET_TAGS);
278 }
279
280 /* Prepend a packet tag. */
281 void
282 m_tag_prepend(struct mbuf *m, struct m_tag *t)
283 {
284 KASSERT(m && t, ("m_tag_prepend: null argument, m %p t %p", m, t));
285 KASSERT(m->m_flags & M_PKTHDR, ("m_tag_prepend: mbuf %p non-header", m));
286 SLIST_INSERT_HEAD(&m->m_pkthdr.tags, t, m_tag_link);
287 }
288
289 /* Unlink a packet tag. */
290 void
291 m_tag_unlink(struct mbuf *m, struct m_tag *t)
292 {
293 KASSERT(m && t, ("m_tag_unlink: null argument, m %p t %p", m, t));
294 KASSERT(m->m_flags & M_PKTHDR, ("m_tag_unlink: mbuf %p non-header", m));
295 SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link);
296 }
297
298 /* Unlink and free a packet tag. */
299 void
300 m_tag_delete(struct mbuf *m, struct m_tag *t)
301 {
302 KASSERT(m && t, ("m_tag_delete: null argument, m %p t %p", m, t));
303 KASSERT(m->m_flags & M_PKTHDR, ("m_tag_delete: mbuf %p non-header", m));
304 m_tag_unlink(m, t);
305 m_tag_free(t);
306 }
307
308 /* Unlink and free a packet tag chain, starting from given tag. */
309 void
310 m_tag_delete_chain(struct mbuf *m)
311 {
312 struct m_tag *p, *q;
313
314 KASSERT(m, ("m_tag_delete_chain: null mbuf"));
315 KASSERT(m->m_flags & M_PKTHDR, ("m_tag_delete_chain: mbuf %p non-header", m));
316 p = SLIST_FIRST(&m->m_pkthdr.tags);
317 if (p == NULL)
318 return;
319 while ((q = SLIST_NEXT(p, m_tag_link)) != NULL)
320 m_tag_delete(m, q);
321 m_tag_delete(m, p);
322 }
323
324 /* Find a tag, starting from a given position. */
325 struct m_tag *
326 m_tag_locate(struct mbuf *m, uint32_t cookie, int type, struct m_tag *t)
327 {
328 struct m_tag *p;
329
330 KASSERT(m, ("m_tag_find: null mbuf"));
331 if (t == NULL)
332 p = SLIST_FIRST(&m->m_pkthdr.tags);
333 else
334 p = SLIST_NEXT(t, m_tag_link);
335 while (p != NULL) {
336 if (p->m_tag_cookie == cookie && p->m_tag_id == type)
337 return p;
338 p = SLIST_NEXT(p, m_tag_link);
339 }
340 return NULL;
341 }
342
343 /* Copy a single tag. */
344 struct m_tag *
345 m_tag_copy(struct m_tag *t, int how)
346 {
347 struct m_tag *p;
348
349 KASSERT(t, ("m_tag_copy: null tag"));
350 p = m_tag_alloc(t->m_tag_cookie, t->m_tag_id, t->m_tag_len, how);
351 if (p == NULL)
352 return (NULL);
353 bcopy(t + 1, p + 1, t->m_tag_len); /* Copy the data */
354 return p;
355 }
356
357 /*
358 * Copy two tag chains. The destination mbuf (to) loses any attached
359 * tags even if the operation fails. This should not be a problem, as
360 * m_tag_copy_chain() is typically called with a newly-allocated
361 * destination mbuf.
362 */
363 int
364 m_tag_copy_chain(struct mbuf *to, const struct mbuf *from, int how)
365 {
366 struct m_tag *p, *t, *tprev = NULL;
367
368 KASSERT(to && from,
369 ("m_tag_copy: null argument, to %p from %p", to, from));
370 m_tag_delete_chain(to);
371 SLIST_FOREACH(p, &from->m_pkthdr.tags, m_tag_link) {
372 t = m_tag_copy(p, how);
373 if (t == NULL) {
374 m_tag_delete_chain(to);
375 return 0;
376 }
377 if (tprev == NULL)
378 SLIST_INSERT_HEAD(&to->m_pkthdr.tags, t, m_tag_link);
379 else {
380 SLIST_INSERT_AFTER(tprev, t, m_tag_link);
381 tprev = t;
382 }
383 }
384 return 1;
385 }
386
387 /* Initialize tags on an mbuf. */
388 void
389 m_tag_init(struct mbuf *m)
390 {
391 SLIST_INIT(&m->m_pkthdr.tags);
392 }
393
394 /* Get first tag in chain. */
395 struct m_tag *
396 m_tag_first(struct mbuf *m)
397 {
398 return SLIST_FIRST(&m->m_pkthdr.tags);
399 }
400
401 /* Get next tag in chain. */
402 struct m_tag *
403 m_tag_next(struct mbuf *m, struct m_tag *t)
404 {
405 return SLIST_NEXT(t, m_tag_link);
406 }
Cache object: 2b1954795c90829a5acebaa5fbec4cff
|