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