1 /* $FreeBSD: src/sys/kern/uipc_mbuf2.c,v 1.2.2.5 2003/01/23 21:06:44 sam Exp $ */ 2 /* $DragonFly: src/sys/kern/uipc_mbuf2.c,v 1.15 2007/10/20 10:28:44 sephe Exp $ */ 3 /* $KAME: uipc_mbuf2.c,v 1.31 2001/11/28 11:08:53 itojun Exp $ */ 4 /* $NetBSD: uipc_mbuf.c,v 1.40 1999/04/01 00:23:25 thorpej Exp $ */ 5 6 /* 7 * Copyright (C) 1999 WIDE Project. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the project nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 /* 36 * Copyright (c) 1982, 1986, 1988, 1991, 1993 37 * The Regents of the University of California. All rights reserved. 38 * 39 * Redistribution and use in source and binary forms, with or without 40 * modification, are permitted provided that the following conditions 41 * are met: 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 3. All advertising materials mentioning features or use of this software 48 * must display the following acknowledgement: 49 * This product includes software developed by the University of 50 * California, Berkeley and its contributors. 51 * 4. Neither the name of the University nor the names of its contributors 52 * may be used to endorse or promote products derived from this software 53 * without specific prior written permission. 54 * 55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 65 * SUCH DAMAGE. 66 * 67 * @(#)uipc_mbuf.c 8.4 (Berkeley) 2/14/95 68 */ 69 70 /*#define PULLDOWN_DEBUG*/ 71 72 #include <sys/param.h> 73 #include <sys/systm.h> 74 #include <sys/kernel.h> 75 #include <sys/proc.h> 76 #include <sys/malloc.h> 77 #include <sys/mbuf.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 on shared cluster (sharedcluster) 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 sharedcluster; 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 kprintf("before:"); 113 for (t = m; t; t = t->m_next) 114 kprintf(" %d", t->m_len); 115 kprintf("\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 sharedcluster = (m_sharecount(n) > 1); 134 135 /* 136 * the target data is on <n, off>. 137 * if we got enough data on the mbuf "n", we're done. 138 */ 139 if ((off == 0 || offp) && len <= n->m_len - off && !sharedcluster) 140 goto ok; 141 142 /* 143 * when len <= n->m_len - off and off != 0, it is a special case. 144 * len bytes from <n, off> sits in single mbuf, but the caller does 145 * not like the starting position (off). 146 * chop the current mbuf into two pieces, set off to 0. 147 */ 148 if (len <= n->m_len - off) { 149 o = m_dup1(n, off, n->m_len - off, MB_DONTWAIT); 150 if (o == NULL) { 151 m_freem(m); 152 return NULL; /* ENOBUFS */ 153 } 154 n->m_len = off; 155 o->m_next = n->m_next; 156 n->m_next = o; 157 n = n->m_next; 158 off = 0; 159 goto ok; 160 } 161 162 /* 163 * we need to take hlen from <n, off> and tlen from <n->m_next, 0>, 164 * and construct contiguous mbuf with m_len == len. 165 * note that hlen + tlen == len, and tlen > 0. 166 */ 167 hlen = n->m_len - off; 168 tlen = len - hlen; 169 170 /* 171 * ensure that we have enough trailing data on mbuf chain. 172 * if not, we can do nothing about the chain. 173 */ 174 olen = 0; 175 for (o = n->m_next; o != NULL; o = o->m_next) 176 olen += o->m_len; 177 if (hlen + olen < len) { 178 m_freem(m); 179 return NULL; /* mbuf chain too short */ 180 } 181 182 /* 183 * easy cases first. 184 * we need to use m_copydata() to get data from <n->m_next, 0>. 185 */ 186 if ((off == 0 || offp) && M_TRAILINGSPACE(n) >= tlen && 187 !sharedcluster) { 188 m_copydata(n->m_next, 0, tlen, mtod(n, caddr_t) + n->m_len); 189 n->m_len += tlen; 190 m_adj(n->m_next, tlen); 191 goto ok; 192 } 193 if ((off == 0 || offp) && M_LEADINGSPACE(n->m_next) >= hlen && 194 !sharedcluster) { 195 n->m_next->m_data -= hlen; 196 n->m_next->m_len += hlen; 197 bcopy(mtod(n, caddr_t) + off, mtod(n->m_next, caddr_t), hlen); 198 n->m_len -= hlen; 199 n = n->m_next; 200 off = 0; 201 goto ok; 202 } 203 204 /* 205 * now, we need to do the hard way. don't m_copy as there's no room 206 * on both end. 207 */ 208 o = m_getl(len, MB_DONTWAIT, m->m_type, 0, NULL); 209 if (!o) { 210 m_freem(m); 211 return NULL; /* ENOBUFS */ 212 } 213 /* get hlen from <n, off> into <o, 0> */ 214 o->m_len = hlen; 215 bcopy(mtod(n, caddr_t) + off, mtod(o, caddr_t), hlen); 216 n->m_len -= hlen; 217 /* get tlen from <n->m_next, 0> into <o, hlen> */ 218 m_copydata(n->m_next, 0, tlen, mtod(o, caddr_t) + o->m_len); 219 o->m_len += tlen; 220 m_adj(n->m_next, tlen); 221 o->m_next = n->m_next; 222 n->m_next = o; 223 n = o; 224 off = 0; 225 226 ok: 227 #ifdef PULLDOWN_DEBUG 228 { 229 struct mbuf *t; 230 kprintf("after:"); 231 for (t = m; t; t = t->m_next) 232 kprintf("%c%d", t == n ? '*' : ' ', t->m_len); 233 kprintf(" (off=%d)\n", off); 234 } 235 #endif 236 if (offp) 237 *offp = off; 238 return n; 239 } 240 241 static struct mbuf * 242 m_dup1(struct mbuf *m, int off, int len, int wait) 243 { 244 struct mbuf *n; 245 246 if (len > MCLBYTES) 247 return NULL; 248 n = m_getl(len, wait, m->m_type, m->m_flags, NULL); 249 if (!n) 250 return NULL; 251 if (off == 0 && (m->m_flags & M_PKTHDR) && !m_dup_pkthdr(n, m, wait)) { 252 m_free(n); 253 return NULL; 254 } 255 m_copydata(m, off, len, mtod(n, caddr_t)); 256 return n; 257 } 258 259 /* Get a packet tag structure along with specified data following. */ 260 struct m_tag * 261 m_tag_alloc(u_int32_t cookie, int type, int len, int wait) 262 { 263 struct m_tag *t; 264 265 if (len < 0) 266 return NULL; 267 t = kmalloc(len + sizeof(struct m_tag), M_PACKET_TAGS, MBTOM(wait)); 268 if (t == NULL) 269 return NULL; 270 t->m_tag_id = type; 271 t->m_tag_len = len; 272 t->m_tag_cookie = cookie; 273 return t; 274 } 275 276 277 /* Free a packet tag. */ 278 void 279 m_tag_free(struct m_tag *t) 280 { 281 kfree(t, M_PACKET_TAGS); 282 } 283 284 /* Prepend a packet tag. */ 285 void 286 m_tag_prepend(struct mbuf *m, struct m_tag *t) 287 { 288 KASSERT(m && t, ("m_tag_prepend: null argument, m %p t %p", m, t)); 289 KASSERT(m->m_flags & M_PKTHDR, ("m_tag_prepend: mbuf %p non-header", m)); 290 SLIST_INSERT_HEAD(&m->m_pkthdr.tags, t, m_tag_link); 291 } 292 293 /* Unlink a packet tag. */ 294 void 295 m_tag_unlink(struct mbuf *m, struct m_tag *t) 296 { 297 KASSERT(m && t, ("m_tag_unlink: null argument, m %p t %p", m, t)); 298 KASSERT(m->m_flags & M_PKTHDR, ("m_tag_unlink: mbuf %p non-header", m)); 299 SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link); 300 } 301 302 /* Unlink and free a packet tag. */ 303 void 304 m_tag_delete(struct mbuf *m, struct m_tag *t) 305 { 306 KASSERT(m && t, ("m_tag_delete: null argument, m %p t %p", m, t)); 307 KASSERT(m->m_flags & M_PKTHDR, ("m_tag_delete: mbuf %p non-header", m)); 308 m_tag_unlink(m, t); 309 m_tag_free(t); 310 } 311 312 /* Unlink and free a packet tag chain, starting from given tag. */ 313 void 314 m_tag_delete_chain(struct mbuf *m) 315 { 316 struct m_tag *p, *q; 317 318 KASSERT(m, ("m_tag_delete_chain: null mbuf")); 319 KASSERT(m->m_flags & M_PKTHDR, ("m_tag_delete_chain: mbuf %p non-header", m)); 320 p = SLIST_FIRST(&m->m_pkthdr.tags); 321 if (p == NULL) 322 return; 323 while ((q = SLIST_NEXT(p, m_tag_link)) != NULL) 324 m_tag_delete(m, q); 325 m_tag_delete(m, p); 326 } 327 328 /* Find a tag, starting from a given position. */ 329 struct m_tag * 330 m_tag_locate(struct mbuf *m, u_int32_t cookie, int type, struct m_tag *t) 331 { 332 struct m_tag *p; 333 334 KASSERT(m, ("m_tag_find: null mbuf")); 335 if (t == NULL) 336 p = SLIST_FIRST(&m->m_pkthdr.tags); 337 else 338 p = SLIST_NEXT(t, m_tag_link); 339 while (p != NULL) { 340 if (p->m_tag_cookie == cookie && p->m_tag_id == type) 341 return p; 342 p = SLIST_NEXT(p, m_tag_link); 343 } 344 return NULL; 345 } 346 347 /* Copy a single tag. */ 348 struct m_tag * 349 m_tag_copy(struct m_tag *t, int how) 350 { 351 struct m_tag *p; 352 353 KASSERT(t, ("m_tag_copy: null tag")); 354 p = m_tag_alloc(t->m_tag_cookie, t->m_tag_id, t->m_tag_len, how); 355 if (p == NULL) 356 return (NULL); 357 bcopy(t + 1, p + 1, t->m_tag_len); /* Copy the data */ 358 return p; 359 } 360 361 /* 362 * Copy two tag chains. The destination mbuf (to) loses any attached 363 * tags even if the operation fails. This should not be a problem, as 364 * m_tag_copy_chain() is typically called with a newly-allocated 365 * destination mbuf. 366 */ 367 int 368 m_tag_copy_chain(struct mbuf *to, const struct mbuf *from, int how) 369 { 370 struct m_tag *p, *t, *tprev = NULL; 371 372 KASSERT(to && from, 373 ("m_tag_copy: null argument, to %p from %p", to, from)); 374 m_tag_delete_chain(to); 375 SLIST_FOREACH(p, &from->m_pkthdr.tags, m_tag_link) { 376 t = m_tag_copy(p, how); 377 if (t == NULL) { 378 m_tag_delete_chain(to); 379 return 0; 380 } 381 if (tprev == NULL) 382 SLIST_INSERT_HEAD(&to->m_pkthdr.tags, t, m_tag_link); 383 else { 384 SLIST_INSERT_AFTER(tprev, t, m_tag_link); 385 tprev = t; 386 } 387 } 388 return 1; 389 } 390 391 /* Initialize tags on an mbuf. */ 392 void 393 m_tag_init(struct mbuf *m) 394 { 395 SLIST_INIT(&m->m_pkthdr.tags); 396 } 397 398 /* Get first tag in chain. */ 399 struct m_tag * 400 m_tag_first(struct mbuf *m) 401 { 402 return SLIST_FIRST(&m->m_pkthdr.tags); 403 } 404 405 /* Get next tag in chain. */ 406 struct m_tag * 407 m_tag_next(struct mbuf *m, struct m_tag *t) 408 { 409 return SLIST_NEXT(t, m_tag_link); 410 } 411