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