FreeBSD/Linux Kernel Cross Reference
sys/kern/uipc_mbuf2.c
1 /* $NetBSD: uipc_mbuf2.c,v 1.18.2.1 2005/05/07 00:09:16 snj Exp $ */
2 /* $KAME: uipc_mbuf2.c,v 1.29 2001/02/14 13:42:10 itojun Exp $ */
3
4 /*
5 * Copyright (C) 1999 WIDE Project.
6 * All rights reserved.
7 *
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.
19 *
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.
31 */
32
33 /*
34 * Copyright (c) 1982, 1986, 1988, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
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.
48 *
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.
60 *
61 * @(#)uipc_mbuf.c 8.4 (Berkeley) 2/14/95
62 */
63
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: uipc_mbuf2.c,v 1.18.2.1 2005/05/07 00:09:16 snj Exp $");
66
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>
72
73 MALLOC_DEFINE(M_PACKET_TAGS, "packet tags", "Packet-attached information");
74
75 /*
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.
80 *
81 * on error return (NULL return value), original "m" will be freed.
82 *
83 * XXX M_TRAILINGSPACE/M_LEADINGSPACE on shared cluster (sharedcluster)
84 */
85 struct mbuf *
86 m_pulldown(struct mbuf *m, int off, int len, int *offp)
87 {
88 struct mbuf *n, *o;
89 int hlen, tlen, olen;
90 int sharedcluster;
91
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 */
98 }
99
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;
106 }
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 */
113 }
114
115 sharedcluster = M_READONLY(n);
116
117 /*
118 * the target data is on <n, off>.
119 * if we got enough data on the mbuf "n", we're done.
120 */
121 if ((off == 0 || offp) && len <= n->m_len - off && !sharedcluster)
122 goto ok;
123
124 /*
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.
129 */
130 if (len <= n->m_len - off) {
131 struct mbuf *mlast;
132
133 o = m_dup(n, off, n->m_len - off, M_DONTWAIT);
134 if (o == NULL) {
135 m_freem(m);
136 return NULL; /* ENOBUFS */
137 }
138 KASSERT(o->m_len >= len);
139 for (mlast = o; mlast->m_next != NULL; mlast = mlast->m_next)
140 ;
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;
147 }
148
149 /*
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.
153 */
154 hlen = n->m_len - off;
155 tlen = len - hlen;
156
157 /*
158 * ensure that we have enough trailing data on mbuf chain.
159 * if not, we can do nothing about the chain.
160 */
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 */
167 }
168
169 /*
170 * easy cases first.
171 * we need to use m_copydata() to get data from <n->m_next, 0>.
172 */
173 if ((off == 0 || offp) && M_TRAILINGSPACE(n) >= tlen &&
174 !sharedcluster) {
175 m_copydata(n->m_next, 0, tlen, mtod(n, caddr_t) + n->m_len);
176 n->m_len += tlen;
177 m_adj(n->m_next, tlen);
178 goto ok;
179 }
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, caddr_t), mtod(n, caddr_t) + off, hlen);
185 n->m_len -= hlen;
186 n = n->m_next;
187 off = 0;
188 goto ok;
189 }
190
191 /*
192 * now, we need to do the hard way. don't m_copy as there's no room
193 * on both end.
194 */
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;
201 }
202 }
203 if (!o) {
204 m_freem(m);
205 return NULL; /* ENOBUFS */
206 }
207 /* get hlen from <n, off> into <o, 0> */
208 o->m_len = hlen;
209 memcpy(mtod(o, caddr_t), mtod(n, caddr_t) + 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, caddr_t) + 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;
219
220 ok:
221 if (offp)
222 *offp = off;
223 return n;
224 }
225
226 /* Get a packet tag structure along with specified data following. */
227 struct m_tag *
228 m_tag_get(int type, int len, int wait)
229 {
230 struct m_tag *t;
231
232 if (len < 0)
233 return (NULL);
234 t = malloc(len + sizeof(struct m_tag), M_PACKET_TAGS, wait);
235 if (t == NULL)
236 return (NULL);
237 t->m_tag_id = type;
238 t->m_tag_len = len;
239 return (t);
240 }
241
242 /* Free a packet tag. */
243 void
244 m_tag_free(struct m_tag *t)
245 {
246
247 free(t, M_PACKET_TAGS);
248 }
249
250 /* Prepend a packet tag. */
251 void
252 m_tag_prepend(struct mbuf *m, struct m_tag *t)
253 {
254
255 SLIST_INSERT_HEAD(&m->m_pkthdr.tags, t, m_tag_link);
256 }
257
258 /* Unlink a packet tag. */
259 void
260 m_tag_unlink(struct mbuf *m, struct m_tag *t)
261 {
262
263 SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link);
264 }
265
266 /* Unlink and free a packet tag. */
267 void
268 m_tag_delete(struct mbuf *m, struct m_tag *t)
269 {
270
271 m_tag_unlink(m, t);
272 m_tag_free(t);
273 }
274
275 /* Unlink and free a packet tag chain, starting from given tag. */
276 __inline void
277 m_tag_delete_chain(struct mbuf *m, struct m_tag *t)
278 {
279 struct m_tag *p, *q;
280
281 if (t != NULL)
282 p = t;
283 else
284 p = SLIST_FIRST(&m->m_pkthdr.tags);
285 if (p == NULL)
286 return;
287 while ((q = SLIST_NEXT(p, m_tag_link)) != NULL)
288 m_tag_delete(m, q);
289 m_tag_delete(m, p);
290 }
291
292 /*
293 * Strip off all tags that would normally vanish when
294 * passing through a network interface. Only persistent
295 * tags will exist after this; these are expected to remain
296 * so long as the mbuf chain exists, regardless of the
297 * path the mbufs take.
298 */
299 void
300 m_tag_delete_nonpersistent(struct mbuf *m)
301 {
302 /* NetBSD has no persistent tags yet, so just delete all tags. */
303 return m_tag_delete_chain(m, NULL);
304 }
305
306
307 /* Find a tag, starting from a given position. */
308 struct m_tag *
309 m_tag_find(struct mbuf *m, int type, struct m_tag *t)
310 {
311 struct m_tag *p;
312
313 if (t == NULL)
314 p = SLIST_FIRST(&m->m_pkthdr.tags);
315 else
316 p = SLIST_NEXT(t, m_tag_link);
317 while (p != NULL) {
318 if (p->m_tag_id == type)
319 return (p);
320 p = SLIST_NEXT(p, m_tag_link);
321 }
322 return (NULL);
323 }
324
325 /* Copy a single tag. */
326 struct m_tag *
327 m_tag_copy(struct m_tag *t)
328 {
329 struct m_tag *p;
330
331 p = m_tag_get(t->m_tag_id, t->m_tag_len, M_NOWAIT);
332 if (p == NULL)
333 return (NULL);
334 bcopy(t + 1, p + 1, t->m_tag_len); /* Copy the data */
335 return (p);
336 }
337
338 /*
339 * Copy two tag chains. The destination mbuf (to) loses any attached
340 * tags even if the operation fails. This should not be a problem, as
341 * m_tag_copy_chain() is typically called with a newly-allocated
342 * destination mbuf.
343 */
344 int
345 m_tag_copy_chain(struct mbuf *to, struct mbuf *from)
346 {
347 struct m_tag *p, *t, *tprev = NULL;
348
349 m_tag_delete_chain(to, NULL);
350 SLIST_FOREACH(p, &from->m_pkthdr.tags, m_tag_link) {
351 t = m_tag_copy(p);
352 if (t == NULL) {
353 m_tag_delete_chain(to, NULL);
354 return (0);
355 }
356 if (tprev == NULL)
357 SLIST_INSERT_HEAD(&to->m_pkthdr.tags, t, m_tag_link);
358 else
359 SLIST_INSERT_AFTER(tprev, t, m_tag_link);
360 tprev = t;
361 }
362 return (1);
363 }
364
365 /* Initialize tags on an mbuf. */
366 void
367 m_tag_init(struct mbuf *m)
368 {
369
370 SLIST_INIT(&m->m_pkthdr.tags);
371 }
372
373 /* Get first tag in chain. */
374 struct m_tag *
375 m_tag_first(struct mbuf *m)
376 {
377
378 return (SLIST_FIRST(&m->m_pkthdr.tags));
379 }
380
381 /* Get next tag in chain. */
382 struct m_tag *
383 m_tag_next(struct mbuf *m, struct m_tag *t)
384 {
385
386 return (SLIST_NEXT(t, m_tag_link));
387 }
Cache object: 9d6d25d92cb1d7fe3cd3e099b37bd38a
|