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