xref: /freebsd/sys/kern/uipc_mbuf2.c (revision f9218d3d4fd34f082473b3a021c6d4d109fb47cf)
1 /*	$FreeBSD$	*/
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 $	*/
4 
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.
20  *
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.
32  */
33 
34 /*
35  * Copyright (c) 1982, 1986, 1988, 1991, 1993
36  *	The Regents of the University of California.  All rights reserved.
37  *
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. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *	This product includes software developed by the University of
49  *	California, Berkeley and its contributors.
50  * 4. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  *	@(#)uipc_mbuf.c	8.4 (Berkeley) 2/14/95
67  */
68 
69 /*#define PULLDOWN_DEBUG*/
70 
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/kernel.h>
74 #include <sys/lock.h>
75 #include <sys/malloc.h>
76 #include <sys/mbuf.h>
77 #include <sys/mutex.h>
78 
79 MALLOC_DEFINE(M_PACKET_TAGS, "tag", "packet-attached information");
80 
81 /* can't call it m_dup(), as freebsd[34] uses m_dup() with different arg */
82 static struct mbuf *m_dup1(struct mbuf *, int, int, int);
83 
84 /*
85  * ensure that [off, off + len) is contiguous on the mbuf chain "m".
86  * packet chain before "off" is kept untouched.
87  * if offp == NULL, the target will start at <retval, 0> on resulting chain.
88  * if offp != NULL, the target will start at <retval, *offp> on resulting chain.
89  *
90  * on error return (NULL return value), original "m" will be freed.
91  *
92  * XXX: M_TRAILINGSPACE/M_LEADINGSPACE only permitted on writable ext_buf.
93  */
94 struct mbuf *
95 m_pulldown(struct mbuf *m, int off, int len, int *offp)
96 {
97 	struct mbuf *n, *o;
98 	int hlen, tlen, olen;
99 	int writable;
100 
101 	/* check invalid arguments. */
102 	if (m == NULL)
103 		panic("m == NULL in m_pulldown()");
104 	if (len > MCLBYTES) {
105 		m_freem(m);
106 		return NULL;	/* impossible */
107 	}
108 
109 #ifdef PULLDOWN_DEBUG
110     {
111 	struct mbuf *t;
112 	printf("before:");
113 	for (t = m; t; t = t->m_next)
114 		printf(" %d", t->m_len);
115 	printf("\n");
116     }
117 #endif
118 	n = m;
119 	while (n != NULL && off > 0) {
120 		if (n->m_len > off)
121 			break;
122 		off -= n->m_len;
123 		n = n->m_next;
124 	}
125 	/* be sure to point non-empty mbuf */
126 	while (n != NULL && n->m_len == 0)
127 		n = n->m_next;
128 	if (!n) {
129 		m_freem(m);
130 		return NULL;	/* mbuf chain too short */
131 	}
132 
133 	/*
134 	 * XXX: This code is flawed because it considers a "writable" mbuf
135 	 *      data region to require all of the following:
136 	 *	  (i) mbuf _has_ to have M_EXT set; if it is just a regular
137 	 *	      mbuf, it is still not considered "writable."
138 	 *	  (ii) since mbuf has M_EXT, the ext_type _has_ to be
139 	 *	       EXT_CLUSTER. Anything else makes it non-writable.
140 	 *	  (iii) M_WRITABLE() must evaluate true.
141 	 *      Ideally, the requirement should only be (iii).
142 	 *
143 	 * If we're writable, we're sure we're writable, because the ref. count
144 	 * cannot increase from 1, as that would require posession of mbuf
145 	 * n by someone else (which is impossible). However, if we're _not_
146 	 * writable, we may eventually become writable )if the ref. count drops
147 	 * to 1), but we'll fail to notice it unless we re-evaluate
148 	 * M_WRITABLE(). For now, we only evaluate once at the beginning and
149 	 * live with this.
150 	 */
151 	/*
152 	 * XXX: This is dumb. If we're just a regular mbuf with no M_EXT,
153 	 *      then we're not "writable," according to this code.
154 	 */
155 	writable = 0;
156 	if ((n->m_flags & M_EXT) == 0 ||
157 	    (n->m_ext.ext_type == EXT_CLUSTER && M_WRITABLE(n)))
158 		writable = 1;
159 
160 	/*
161 	 * the target data is on <n, off>.
162 	 * if we got enough data on the mbuf "n", we're done.
163 	 */
164 	if ((off == 0 || offp) && len <= n->m_len - off && writable)
165 		goto ok;
166 
167 	/*
168 	 * when len <= n->m_len - off and off != 0, it is a special case.
169 	 * len bytes from <n, off> sits in single mbuf, but the caller does
170 	 * not like the starting position (off).
171 	 * chop the current mbuf into two pieces, set off to 0.
172 	 */
173 	if (len <= n->m_len - off) {
174 		o = m_dup1(n, off, n->m_len - off, M_DONTWAIT);
175 		if (o == NULL) {
176 			m_freem(m);
177 			return NULL;	/* ENOBUFS */
178 		}
179 		n->m_len = off;
180 		o->m_next = n->m_next;
181 		n->m_next = o;
182 		n = n->m_next;
183 		off = 0;
184 		goto ok;
185 	}
186 
187 	/*
188 	 * we need to take hlen from <n, off> and tlen from <n->m_next, 0>,
189 	 * and construct contiguous mbuf with m_len == len.
190 	 * note that hlen + tlen == len, and tlen > 0.
191 	 */
192 	hlen = n->m_len - off;
193 	tlen = len - hlen;
194 
195 	/*
196 	 * ensure that we have enough trailing data on mbuf chain.
197 	 * if not, we can do nothing about the chain.
198 	 */
199 	olen = 0;
200 	for (o = n->m_next; o != NULL; o = o->m_next)
201 		olen += o->m_len;
202 	if (hlen + olen < len) {
203 		m_freem(m);
204 		return NULL;	/* mbuf chain too short */
205 	}
206 
207 	/*
208 	 * easy cases first.
209 	 * we need to use m_copydata() to get data from <n->m_next, 0>.
210 	 */
211 	if ((off == 0 || offp) && M_TRAILINGSPACE(n) >= tlen
212 	 && writable) {
213 		m_copydata(n->m_next, 0, tlen, mtod(n, caddr_t) + n->m_len);
214 		n->m_len += tlen;
215 		m_adj(n->m_next, tlen);
216 		goto ok;
217 	}
218 	if ((off == 0 || offp) && M_LEADINGSPACE(n->m_next) >= hlen
219 	 && writable) {
220 		n->m_next->m_data -= hlen;
221 		n->m_next->m_len += hlen;
222 		bcopy(mtod(n, caddr_t) + off, mtod(n->m_next, caddr_t), hlen);
223 		n->m_len -= hlen;
224 		n = n->m_next;
225 		off = 0;
226 		goto ok;
227 	}
228 
229 	/*
230 	 * now, we need to do the hard way.  don't m_copy as there's no room
231 	 * on both end.
232 	 */
233 	MGET(o, M_DONTWAIT, m->m_type);
234 	if (o && len > MLEN) {
235 		MCLGET(o, M_DONTWAIT);
236 		if ((o->m_flags & M_EXT) == 0) {
237 			m_free(o);
238 			o = NULL;
239 		}
240 	}
241 	if (!o) {
242 		m_freem(m);
243 		return NULL;	/* ENOBUFS */
244 	}
245 	/* get hlen from <n, off> into <o, 0> */
246 	o->m_len = hlen;
247 	bcopy(mtod(n, caddr_t) + off, mtod(o, caddr_t), hlen);
248 	n->m_len -= hlen;
249 	/* get tlen from <n->m_next, 0> into <o, hlen> */
250 	m_copydata(n->m_next, 0, tlen, mtod(o, caddr_t) + o->m_len);
251 	o->m_len += tlen;
252 	m_adj(n->m_next, tlen);
253 	o->m_next = n->m_next;
254 	n->m_next = o;
255 	n = o;
256 	off = 0;
257 
258 ok:
259 #ifdef PULLDOWN_DEBUG
260     {
261 	struct mbuf *t;
262 	printf("after:");
263 	for (t = m; t; t = t->m_next)
264 		printf("%c%d", t == n ? '*' : ' ', t->m_len);
265 	printf(" (off=%d)\n", off);
266     }
267 #endif
268 	if (offp)
269 		*offp = off;
270 	return n;
271 }
272 
273 static struct mbuf *
274 m_dup1(struct mbuf *m, int off, int len, int wait)
275 {
276 	struct mbuf *n;
277 	int l;
278 	int copyhdr;
279 
280 	if (len > MCLBYTES)
281 		return NULL;
282 	if (off == 0 && (m->m_flags & M_PKTHDR) != 0) {
283 		copyhdr = 1;
284 		MGETHDR(n, wait, m->m_type);
285 		l = MHLEN;
286 	} else {
287 		copyhdr = 0;
288 		MGET(n, wait, m->m_type);
289 		l = MLEN;
290 	}
291 	if (n && len > l) {
292 		MCLGET(n, wait);
293 		if ((n->m_flags & M_EXT) == 0) {
294 			m_free(n);
295 			n = NULL;
296 		}
297 	}
298 	if (!n)
299 		return NULL;
300 
301 	if (copyhdr && !m_dup_pkthdr(n, m, wait)) {
302 		m_free(n);
303 		return NULL;
304 	}
305 	m_copydata(m, off, len, mtod(n, caddr_t));
306 	return n;
307 }
308 
309 /* Get a packet tag structure along with specified data following. */
310 struct m_tag *
311 m_tag_alloc(u_int32_t cookie, int type, int len, int wait)
312 {
313 	struct m_tag *t;
314 
315 	if (len < 0)
316 		return NULL;
317 	t = malloc(len + sizeof(struct m_tag), M_PACKET_TAGS, wait);
318 	if (t == NULL)
319 		return NULL;
320 	t->m_tag_id = type;
321 	t->m_tag_len = len;
322 	t->m_tag_cookie = cookie;
323 	return t;
324 }
325 
326 /* Free a packet tag. */
327 void
328 m_tag_free(struct m_tag *t)
329 {
330 	free(t, M_PACKET_TAGS);
331 }
332 
333 /* Prepend a packet tag. */
334 void
335 m_tag_prepend(struct mbuf *m, struct m_tag *t)
336 {
337 	KASSERT(m && t, ("m_tag_prepend: null argument, m %p t %p", m, t));
338 	SLIST_INSERT_HEAD(&m->m_pkthdr.tags, t, m_tag_link);
339 }
340 
341 /* Unlink a packet tag. */
342 void
343 m_tag_unlink(struct mbuf *m, struct m_tag *t)
344 {
345 	KASSERT(m && t, ("m_tag_unlink: null argument, m %p t %p", m, t));
346 	SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link);
347 }
348 
349 /* Unlink and free a packet tag. */
350 void
351 m_tag_delete(struct mbuf *m, struct m_tag *t)
352 {
353 	KASSERT(m && t, ("m_tag_delete: null argument, m %p t %p", m, t));
354 	m_tag_unlink(m, t);
355 	m_tag_free(t);
356 }
357 
358 /* Unlink and free a packet tag chain, starting from given tag. */
359 void
360 m_tag_delete_chain(struct mbuf *m, struct m_tag *t)
361 {
362 	struct m_tag *p, *q;
363 
364 	KASSERT(m, ("m_tag_delete_chain: null mbuf"));
365 	if (t != NULL)
366 		p = t;
367 	else
368 		p = SLIST_FIRST(&m->m_pkthdr.tags);
369 	if (p == NULL)
370 		return;
371 	while ((q = SLIST_NEXT(p, m_tag_link)) != NULL)
372 		m_tag_delete(m, q);
373 	m_tag_delete(m, p);
374 }
375 
376 /* Find a tag, starting from a given position. */
377 struct m_tag *
378 m_tag_locate(struct mbuf *m, u_int32_t cookie, int type, struct m_tag *t)
379 {
380 	struct m_tag *p;
381 
382 	KASSERT(m, ("m_tag_locate: null mbuf"));
383 	if (t == NULL)
384 		p = SLIST_FIRST(&m->m_pkthdr.tags);
385 	else
386 		p = SLIST_NEXT(t, m_tag_link);
387 	while (p != NULL) {
388 		if (p->m_tag_cookie == cookie && p->m_tag_id == type)
389 			return p;
390 		p = SLIST_NEXT(p, m_tag_link);
391 	}
392 	return NULL;
393 }
394 
395 /* Copy a single tag. */
396 struct m_tag *
397 m_tag_copy(struct m_tag *t, int how)
398 {
399 	struct m_tag *p;
400 
401 	KASSERT(t, ("m_tag_copy: null tag"));
402 	p = m_tag_alloc(t->m_tag_cookie, t->m_tag_id, t->m_tag_len, how);
403 	if (p == NULL)
404 		return (NULL);
405 	bcopy(t + 1, p + 1, t->m_tag_len); /* Copy the data */
406 	return p;
407 }
408 
409 /*
410  * Copy two tag chains. The destination mbuf (to) loses any attached
411  * tags even if the operation fails. This should not be a problem, as
412  * m_tag_copy_chain() is typically called with a newly-allocated
413  * destination mbuf.
414  */
415 int
416 m_tag_copy_chain(struct mbuf *to, struct mbuf *from, int how)
417 {
418 	struct m_tag *p, *t, *tprev = NULL;
419 
420 	KASSERT(to && from,
421 		("m_tag_copy_chain: null argument, to %p from %p", to, from));
422 	m_tag_delete_chain(to, NULL);
423 	SLIST_FOREACH(p, &from->m_pkthdr.tags, m_tag_link) {
424 		t = m_tag_copy(p, how);
425 		if (t == NULL) {
426 			m_tag_delete_chain(to, NULL);
427 			return 0;
428 		}
429 		if (tprev == NULL)
430 			SLIST_INSERT_HEAD(&to->m_pkthdr.tags, t, m_tag_link);
431 		else
432 			SLIST_INSERT_AFTER(tprev, t, m_tag_link);
433 		tprev = t;
434 	}
435 	return 1;
436 }
437 
438 /* Initialize tags on an mbuf. */
439 void
440 m_tag_init(struct mbuf *m)
441 {
442 	SLIST_INIT(&m->m_pkthdr.tags);
443 }
444 
445 /* Get first tag in chain. */
446 struct m_tag *
447 m_tag_first(struct mbuf *m)
448 {
449 	return SLIST_FIRST(&m->m_pkthdr.tags);
450 }
451 
452 /* Get next tag in chain. */
453 struct m_tag *
454 m_tag_next(struct mbuf *m, struct m_tag *t)
455 {
456 	return SLIST_NEXT(t, m_tag_link);
457 }
458