No empty .Rs/.Re
[netbsd-mini2440.git] / sys / kern / uipc_mbuf2.c
blobe107c69b02a161161c91b17066789962c3c9430b
1 /* $NetBSD: uipc_mbuf2.c,v 1.27 2009/03/18 17:06:51 cegger Exp $ */
2 /* $KAME: uipc_mbuf2.c,v 1.29 2001/02/14 13:42:10 itojun Exp $ */
4 /*
5 * Copyright (C) 1999 WIDE Project.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
34 * Copyright (c) 1982, 1986, 1988, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
61 * @(#)uipc_mbuf.c 8.4 (Berkeley) 2/14/95
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: uipc_mbuf2.c,v 1.27 2009/03/18 17:06:51 cegger Exp $");
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/proc.h>
70 #include <sys/malloc.h>
71 #include <sys/mbuf.h>
73 MALLOC_DEFINE(M_PACKET_TAGS, "packet tags", "Packet-attached information");
76 * ensure that [off, off + len) is contiguous on the mbuf chain "m".
77 * packet chain before "off" is kept untouched.
78 * if offp == NULL, the target will start at <retval, 0> on resulting chain.
79 * if offp != NULL, the target will start at <retval, *offp> on resulting chain.
81 * on error return (NULL return value), original "m" will be freed.
83 * XXX M_TRAILINGSPACE/M_LEADINGSPACE on shared cluster (sharedcluster)
85 struct mbuf *
86 m_pulldown(struct mbuf *m, int off, int len, int *offp)
88 struct mbuf *n, *o;
89 int hlen, tlen, olen;
90 int sharedcluster;
92 /* check invalid arguments. */
93 if (m == NULL)
94 panic("m == NULL in m_pulldown()");
95 if (len > MCLBYTES) {
96 m_freem(m);
97 return NULL; /* impossible */
100 n = m;
101 while (n != NULL && off > 0) {
102 if (n->m_len > off)
103 break;
104 off -= n->m_len;
105 n = n->m_next;
107 /* be sure to point non-empty mbuf */
108 while (n != NULL && n->m_len == 0)
109 n = n->m_next;
110 if (!n) {
111 m_freem(m);
112 return NULL; /* mbuf chain too short */
115 sharedcluster = M_READONLY(n);
118 * the target data is on <n, off>.
119 * if we got enough data on the mbuf "n", we're done.
121 if ((off == 0 || offp) && len <= n->m_len - off && !sharedcluster)
122 goto ok;
125 * when len <= n->m_len - off and off != 0, it is a special case.
126 * len bytes from <n, off> sits in single mbuf, but the caller does
127 * not like the starting position (off).
128 * chop the current mbuf into two pieces, set off to 0.
130 if (len <= n->m_len - off) {
131 struct mbuf *mlast;
133 o = m_dup(n, off, n->m_len - off, M_DONTWAIT);
134 if (o == NULL) {
135 m_freem(m);
136 return NULL; /* ENOBUFS */
138 KASSERT(o->m_len >= len);
139 for (mlast = o; mlast->m_next != NULL; mlast = mlast->m_next)
141 n->m_len = off;
142 mlast->m_next = n->m_next;
143 n->m_next = o;
144 n = o;
145 off = 0;
146 goto ok;
150 * we need to take hlen from <n, off> and tlen from <n->m_next, 0>,
151 * and construct contiguous mbuf with m_len == len.
152 * note that hlen + tlen == len, and tlen > 0.
154 hlen = n->m_len - off;
155 tlen = len - hlen;
158 * ensure that we have enough trailing data on mbuf chain.
159 * if not, we can do nothing about the chain.
161 olen = 0;
162 for (o = n->m_next; o != NULL; o = o->m_next)
163 olen += o->m_len;
164 if (hlen + olen < len) {
165 m_freem(m);
166 return NULL; /* mbuf chain too short */
170 * easy cases first.
171 * we need to use m_copydata() to get data from <n->m_next, 0>.
173 if ((off == 0 || offp) && M_TRAILINGSPACE(n) >= tlen &&
174 !sharedcluster) {
175 m_copydata(n->m_next, 0, tlen, mtod(n, char *) + n->m_len);
176 n->m_len += tlen;
177 m_adj(n->m_next, tlen);
178 goto ok;
180 if ((off == 0 || offp) && M_LEADINGSPACE(n->m_next) >= hlen &&
181 !sharedcluster && n->m_next->m_len >= tlen) {
182 n->m_next->m_data -= hlen;
183 n->m_next->m_len += hlen;
184 memcpy(mtod(n->m_next, void *), mtod(n, char *) + off, hlen);
185 n->m_len -= hlen;
186 n = n->m_next;
187 off = 0;
188 goto ok;
192 * now, we need to do the hard way. don't m_copy as there's no room
193 * on both end.
195 MGET(o, M_DONTWAIT, m->m_type);
196 if (o && len > MLEN) {
197 MCLGET(o, M_DONTWAIT);
198 if ((o->m_flags & M_EXT) == 0) {
199 m_free(o);
200 o = NULL;
203 if (!o) {
204 m_freem(m);
205 return NULL; /* ENOBUFS */
207 /* get hlen from <n, off> into <o, 0> */
208 o->m_len = hlen;
209 memcpy(mtod(o, void *), mtod(n, char *) + off, hlen);
210 n->m_len -= hlen;
211 /* get tlen from <n->m_next, 0> into <o, hlen> */
212 m_copydata(n->m_next, 0, tlen, mtod(o, char *) + o->m_len);
213 o->m_len += tlen;
214 m_adj(n->m_next, tlen);
215 o->m_next = n->m_next;
216 n->m_next = o;
217 n = o;
218 off = 0;
221 if (offp)
222 *offp = off;
223 return n;
227 * FreeBSD 4.6 introduced m_getcl(), which performs `fast' allocation
228 * mbuf clusters from a cache of recently-freed clusters. (If the cache
229 * is empty, new clusters are allocated en-masse).
230 * On NetBSD, for now, implement the `cache' as a function
231 * using normal NetBSD mbuf/cluster allocation macros. Replace this
232 * with fast-cache code, if and when NetBSD implements one.
234 struct mbuf *
235 m_getcl(int how, int type, int flags)
237 struct mbuf *mp;
239 if ((flags & M_PKTHDR) != 0)
240 MGETHDR(mp, how, type);
241 else
242 MGET(mp, how, type);
244 if (mp == NULL)
245 return NULL;
247 MCLGET(mp, how);
248 if ((mp->m_flags & M_EXT) != 0)
249 return mp;
251 m_free(mp);
252 return NULL;
255 /* Get a packet tag structure along with specified data following. */
256 struct m_tag *
257 m_tag_get(int type, int len, int wait)
259 struct m_tag *t;
261 if (len < 0)
262 return (NULL);
263 t = malloc(len + sizeof(struct m_tag), M_PACKET_TAGS, wait);
264 if (t == NULL)
265 return (NULL);
266 t->m_tag_id = type;
267 t->m_tag_len = len;
268 return (t);
271 /* Free a packet tag. */
272 void
273 m_tag_free(struct m_tag *t)
276 free(t, M_PACKET_TAGS);
279 /* Prepend a packet tag. */
280 void
281 m_tag_prepend(struct mbuf *m, struct m_tag *t)
284 SLIST_INSERT_HEAD(&m->m_pkthdr.tags, t, m_tag_link);
287 /* Unlink a packet tag. */
288 void
289 m_tag_unlink(struct mbuf *m, struct m_tag *t)
292 SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link);
295 /* Unlink and free a packet tag. */
296 void
297 m_tag_delete(struct mbuf *m, struct m_tag *t)
300 m_tag_unlink(m, t);
301 m_tag_free(t);
304 /* Unlink and free a packet tag chain, starting from given tag. */
305 void
306 m_tag_delete_chain(struct mbuf *m, struct m_tag *t)
308 struct m_tag *p, *q;
310 if (t != NULL)
311 p = t;
312 else
313 p = SLIST_FIRST(&m->m_pkthdr.tags);
314 if (p == NULL)
315 return;
316 while ((q = SLIST_NEXT(p, m_tag_link)) != NULL)
317 m_tag_delete(m, q);
318 m_tag_delete(m, p);
322 * Strip off all tags that would normally vanish when
323 * passing through a network interface. Only persistent
324 * tags will exist after this; these are expected to remain
325 * so long as the mbuf chain exists, regardless of the
326 * path the mbufs take.
328 void
329 m_tag_delete_nonpersistent(struct mbuf *m)
331 /* NetBSD has no persistent tags yet, so just delete all tags. */
332 m_tag_delete_chain(m, NULL);
336 /* Find a tag, starting from a given position. */
337 struct m_tag *
338 m_tag_find(struct mbuf *m, int type, struct m_tag *t)
340 struct m_tag *p;
342 if (t == NULL)
343 p = SLIST_FIRST(&m->m_pkthdr.tags);
344 else
345 p = SLIST_NEXT(t, m_tag_link);
346 while (p != NULL) {
347 if (p->m_tag_id == type)
348 return (p);
349 p = SLIST_NEXT(p, m_tag_link);
351 return (NULL);
354 /* Copy a single tag. */
355 struct m_tag *
356 m_tag_copy(struct m_tag *t)
358 struct m_tag *p;
360 p = m_tag_get(t->m_tag_id, t->m_tag_len, M_NOWAIT);
361 if (p == NULL)
362 return (NULL);
363 memcpy(p + 1, t + 1, t->m_tag_len); /* Copy the data */
364 return (p);
368 * Copy two tag chains. The destination mbuf (to) loses any attached
369 * tags even if the operation fails. This should not be a problem, as
370 * m_tag_copy_chain() is typically called with a newly-allocated
371 * destination mbuf.
374 m_tag_copy_chain(struct mbuf *to, struct mbuf *from)
376 struct m_tag *p, *t, *tprev = NULL;
378 m_tag_delete_chain(to, NULL);
379 SLIST_FOREACH(p, &from->m_pkthdr.tags, m_tag_link) {
380 t = m_tag_copy(p);
381 if (t == NULL) {
382 m_tag_delete_chain(to, NULL);
383 return (0);
385 if (tprev == NULL)
386 SLIST_INSERT_HEAD(&to->m_pkthdr.tags, t, m_tag_link);
387 else
388 SLIST_INSERT_AFTER(tprev, t, m_tag_link);
389 tprev = t;
391 return (1);
394 /* Initialize tags on an mbuf. */
395 void
396 m_tag_init(struct mbuf *m)
399 SLIST_INIT(&m->m_pkthdr.tags);
402 /* Get first tag in chain. */
403 struct m_tag *
404 m_tag_first(struct mbuf *m)
407 return (SLIST_FIRST(&m->m_pkthdr.tags));
410 /* Get next tag in chain. */
411 struct m_tag *
412 m_tag_next(struct mbuf *m, struct m_tag *t)
415 return (SLIST_NEXT(t, m_tag_link));