xref: /freebsd/sys/kern/uipc_mbuf2.c (revision 99e8005137088aafb1350e23b113d69b01b0820f)
1 /*	$FreeBSD$	*/
2 /*	$KAME: uipc_mbuf2.c,v 1.15 2000/02/22 14:01:37 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/lock.h>
74 #include <sys/malloc.h>
75 #include <sys/mbuf.h>
76 #include <sys/mutex.h>
77 
78 /*
79  * ensure that [off, off + len) is contiguous on the mbuf chain "m".
80  * packet chain before "off" is kept untouched.
81  * if offp == NULL, the target will start at <retval, 0> on resulting chain.
82  * if offp != NULL, the target will start at <retval, *offp> on resulting chain.
83  *
84  * on error return (NULL return value), original "m" will be freed.
85  *
86  * XXX: M_TRAILINGSPACE/M_LEADINGSPACE only permitted on writable ext_buf.
87  */
88 struct mbuf *
89 m_pulldown(struct mbuf *m, int off, int len, int *offp)
90 {
91 	struct mbuf *n, *o;
92 	int hlen, tlen, olen;
93 	int writable;
94 
95 	/* check invalid arguments. */
96 	if (m == NULL)
97 		panic("m == NULL in m_pulldown()");
98 	if (len > MCLBYTES) {
99 		m_freem(m);
100 		return NULL;	/* impossible */
101 	}
102 
103 #ifdef PULLDOWN_DEBUG
104     {
105 	struct mbuf *t;
106 	printf("before:");
107 	for (t = m; t; t = t->m_next)
108 		printf(" %d", t->m_len);
109 	printf("\n");
110     }
111 #endif
112 	n = m;
113 	while (n != NULL && off > 0) {
114 		if (n->m_len > off)
115 			break;
116 		off -= n->m_len;
117 		n = n->m_next;
118 	}
119 	/* be sure to point non-empty mbuf */
120 	while (n != NULL && n->m_len == 0)
121 		n = n->m_next;
122 	if (!n) {
123 		m_freem(m);
124 		return NULL;	/* mbuf chain too short */
125 	}
126 
127 	/*
128 	 * the target data is on <n, off>.
129 	 * if we got enough data on the mbuf "n", we're done.
130 	 */
131 	if ((off == 0 || offp) && len <= n->m_len - off)
132 		goto ok;
133 
134 	/*
135 	 * when len < n->m_len - off and off != 0, it is a special case.
136 	 * len bytes from <n, off> sits in single mbuf, but the caller does
137 	 * not like the starting position (off).
138 	 * chop the current mbuf into two pieces, set off to 0.
139 	 */
140 	if (len < n->m_len - off) {
141 		o = m_copym(n, off, n->m_len - off, M_DONTWAIT);
142 		if (o == NULL) {
143 			m_freem(m);
144 			return NULL;	/* ENOBUFS */
145 		}
146 		n->m_len = off;
147 		o->m_next = n->m_next;
148 		n->m_next = o;
149 		n = n->m_next;
150 		off = 0;
151 		goto ok;
152 	}
153 
154 	/*
155 	 * we need to take hlen from <n, off> and tlen from <n->m_next, 0>,
156 	 * and construct contiguous mbuf with m_len == len.
157 	 * note that hlen + tlen == len, and tlen > 0.
158 	 */
159 	hlen = n->m_len - off;
160 	tlen = len - hlen;
161 
162 	/*
163 	 * ensure that we have enough trailing data on mbuf chain.
164 	 * if not, we can do nothing about the chain.
165 	 */
166 	olen = 0;
167 	for (o = n->m_next; o != NULL; o = o->m_next)
168 		olen += o->m_len;
169 	if (hlen + olen < len) {
170 		m_freem(m);
171 		return NULL;	/* mbuf chain too short */
172 	}
173 
174 	/*
175 	 * easy cases first.
176 	 * we need to use m_copydata() to get data from <n->m_next, 0>.
177 	 */
178 	/*
179 	 * XXX: This code is flawed because it considers a "writable" mbuf
180 	 *      data region to require all of the following:
181 	 *	  (i) mbuf _has_ to have M_EXT set; if it is just a regular
182 	 *	      mbuf, it is still not considered "writable."
183 	 *	  (ii) since mbuf has M_EXT, the ext_type _has_ to be
184 	 *	       EXT_CLUSTER. Anything else makes it non-writable.
185 	 *	  (iii) M_WRITABLE() must evaluate true.
186 	 *      Ideally, the requirement should only be (iii).
187 	 *
188 	 * If we're writable, we're sure we're writable, because the ref. count
189 	 * cannot increase from 1, as that would require posession of mbuf
190 	 * n by someone else (which is impossible). However, if we're _not_
191 	 * writable, we may eventually become writable )if the ref. count drops
192 	 * to 1), but we'll fail to notice it unless we re-evaluate
193 	 * M_WRITABLE(). For now, we only evaluate once at the beginning and
194 	 * live with this.
195 	 */
196 	/*
197 	 * XXX: This is dumb. If we're just a regular mbuf with no M_EXT,
198 	 *      then we're not "writable," according to this code.
199 	 */
200 	writable = 0;
201 	if ((n->m_flags & M_EXT) && (n->m_ext.ext_type == EXT_CLUSTER) &&
202 	    M_WRITABLE(n))
203 		writable = 1;
204 
205 	if ((off == 0 || offp) && M_TRAILINGSPACE(n) >= tlen
206 	 && writable) {
207 		m_copydata(n->m_next, 0, tlen, mtod(n, caddr_t) + n->m_len);
208 		n->m_len += tlen;
209 		m_adj(n->m_next, tlen);
210 		goto ok;
211 	}
212 	if ((off == 0 || offp) && M_LEADINGSPACE(n->m_next) >= hlen
213 	 && writable) {
214 		n->m_next->m_data -= hlen;
215 		n->m_next->m_len += hlen;
216 		bcopy(mtod(n, caddr_t) + off, mtod(n->m_next, caddr_t), hlen);
217 		n->m_len -= hlen;
218 		n = n->m_next;
219 		off = 0;
220 		goto ok;
221 	}
222 
223 	/*
224 	 * now, we need to do the hard way.  don't m_copy as there's no room
225 	 * on both end.
226 	 */
227 	MGET(o, M_DONTWAIT, m->m_type);
228 	if (o == NULL) {
229 		m_freem(m);
230 		return NULL;	/* ENOBUFS */
231 	}
232 	if (len > MHLEN) {	/* use MHLEN just for safety */
233 		MCLGET(o, M_DONTWAIT);
234 		if ((o->m_flags & M_EXT) == 0) {
235 			m_freem(m);
236 			m_free(o);
237 			return NULL;	/* ENOBUFS */
238 		}
239 	}
240 	/* get hlen from <n, off> into <o, 0> */
241 	o->m_len = hlen;
242 	bcopy(mtod(n, caddr_t) + off, mtod(o, caddr_t), hlen);
243 	n->m_len -= hlen;
244 	/* get tlen from <n->m_next, 0> into <o, hlen> */
245 	m_copydata(n->m_next, 0, tlen, mtod(o, caddr_t) + o->m_len);
246 	o->m_len += tlen;
247 	m_adj(n->m_next, tlen);
248 	o->m_next = n->m_next;
249 	n->m_next = o;
250 	n = o;
251 	off = 0;
252 
253 ok:
254 #ifdef PULLDOWN_DEBUG
255     {
256 	struct mbuf *t;
257 	printf("after:");
258 	for (t = m; t; t = t->m_next)
259 		printf("%c%d", t == n ? '*' : ' ', t->m_len);
260 	printf(" (off=%d)\n", off);
261     }
262 #endif
263 	if (offp)
264 		*offp = off;
265 	return n;
266 }
267 
268 /*
269  * pkthdr.aux chain manipulation.
270  * we don't allow clusters at this moment.
271  */
272 struct mbuf *
273 m_aux_add(struct mbuf *m, int af, int type)
274 {
275 	struct mbuf *n;
276 	struct mauxtag *t;
277 
278 	if ((m->m_flags & M_PKTHDR) == 0)
279 		return NULL;
280 
281 	n = m_aux_find(m, af, type);
282 	if (n)
283 		return n;
284 
285 	MGET(n, M_DONTWAIT, m->m_type);
286 	if (n == NULL)
287 		return NULL;
288 
289 	t = mtod(n, struct mauxtag *);
290 	t->af = af;
291 	t->type = type;
292 	n->m_data += sizeof(struct mauxtag);
293 	n->m_len = 0;
294 	n->m_next = m->m_pkthdr.aux;
295 	m->m_pkthdr.aux = n;
296 	return n;
297 }
298 
299 struct mbuf *
300 m_aux_find(struct mbuf *m, int af, int type)
301 {
302 	struct mbuf *n;
303 	struct mauxtag *t;
304 
305 	if ((m->m_flags & M_PKTHDR) == 0)
306 		return NULL;
307 
308 	for (n = m->m_pkthdr.aux; n; n = n->m_next) {
309 		t = (struct mauxtag *)n->m_dat;
310 		if (t->af == af && t->type == type)
311 			return n;
312 	}
313 	return NULL;
314 }
315 
316 void
317 m_aux_delete(struct mbuf *m, struct mbuf *victim)
318 {
319 	struct mbuf *n, *prev, *next;
320 	struct mauxtag *t;
321 
322 	if ((m->m_flags & M_PKTHDR) == 0)
323 		return;
324 
325 	prev = NULL;
326 	n = m->m_pkthdr.aux;
327 	while (n) {
328 		t = (struct mauxtag *)n->m_dat;
329 		next = n->m_next;
330 		if (n == victim) {
331 			if (prev)
332 				prev->m_next = n->m_next;
333 			else
334 				m->m_pkthdr.aux = n->m_next;
335 			n->m_next = NULL;
336 			m_free(n);
337 		} else
338 			prev = n;
339 		n = next;
340 	}
341 }
342