34 35#include "opt_mac.h" 36#include "opt_param.h" 37#include "opt_mbuf_stress_test.h" 38 39#include <sys/param.h> 40#include <sys/systm.h> 41#include <sys/kernel.h> 42#include <sys/limits.h> 43#include <sys/lock.h> 44#include <sys/mac.h> 45#include <sys/malloc.h> 46#include <sys/mbuf.h> 47#include <sys/sysctl.h> 48#include <sys/domain.h> 49#include <sys/protosw.h> 50#include <sys/uio.h> 51 52int max_linkhdr; 53int max_protohdr; 54int max_hdr; 55int max_datalen; 56#ifdef MBUF_STRESS_TEST 57int m_defragpackets; 58int m_defragbytes; 59int m_defraguseless; 60int m_defragfailure; 61int m_defragrandomfailures; 62#endif 63 64/* 65 * sysctl(8) exported objects 66 */ 67SYSCTL_DECL(_kern_ipc); 68SYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW, 69 &max_linkhdr, 0, ""); 70SYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW, 71 &max_protohdr, 0, ""); 72SYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, ""); 73SYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW, 74 &max_datalen, 0, ""); 75#ifdef MBUF_STRESS_TEST 76SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragpackets, CTLFLAG_RD, 77 &m_defragpackets, 0, ""); 78SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragbytes, CTLFLAG_RD, 79 &m_defragbytes, 0, ""); 80SYSCTL_INT(_kern_ipc, OID_AUTO, m_defraguseless, CTLFLAG_RD, 81 &m_defraguseless, 0, ""); 82SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragfailure, CTLFLAG_RD, 83 &m_defragfailure, 0, ""); 84SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragrandomfailures, CTLFLAG_RW, 85 &m_defragrandomfailures, 0, ""); 86#endif 87 88/* 89 * Allocate a given length worth of mbufs and/or clusters (whatever fits 90 * best) and return a pointer to the top of the allocated chain. If an 91 * existing mbuf chain is provided, then we will append the new chain 92 * to the existing one but still return the top of the newly allocated 93 * chain. 94 */ 95struct mbuf * 96m_getm(struct mbuf *m, int len, int how, short type) 97{ 98 struct mbuf *mb, *top, *cur, *mtail; 99 int num, rem; 100 int i; 101 102 KASSERT(len >= 0, ("m_getm(): len is < 0")); 103 104 /* If m != NULL, we will append to the end of that chain. */ 105 if (m != NULL) 106 for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next); 107 else 108 mtail = NULL; 109 110 /* 111 * Calculate how many mbufs+clusters ("packets") we need and how much 112 * leftover there is after that and allocate the first mbuf+cluster 113 * if required. 114 */ 115 num = len / MCLBYTES; 116 rem = len % MCLBYTES; 117 top = cur = NULL; 118 if (num > 0) { 119 if ((top = cur = m_getcl(how, type, 0)) == NULL) 120 goto failed; 121 top->m_len = 0; 122 } 123 num--; 124 125 for (i = 0; i < num; i++) { 126 mb = m_getcl(how, type, 0); 127 if (mb == NULL) 128 goto failed; 129 mb->m_len = 0; 130 cur = (cur->m_next = mb); 131 } 132 if (rem > 0) { 133 mb = (rem > MINCLSIZE) ? 134 m_getcl(how, type, 0) : m_get(how, type); 135 if (mb == NULL) 136 goto failed; 137 mb->m_len = 0; 138 if (cur == NULL) 139 top = mb; 140 else 141 cur->m_next = mb; 142 } 143 144 if (mtail != NULL) 145 mtail->m_next = top; 146 return top; 147failed: 148 if (top != NULL) 149 m_freem(top); 150 return NULL; 151} 152 153/* 154 * Free an entire chain of mbufs and associated external buffers, if 155 * applicable. 156 */ 157void 158m_freem(struct mbuf *mb) 159{ 160 161 while (mb != NULL) 162 mb = m_free(mb); 163} 164 165/*- 166 * Configure a provided mbuf to refer to the provided external storage 167 * buffer and setup a reference count for said buffer. If the setting 168 * up of the reference count fails, the M_EXT bit will not be set. If 169 * successfull, the M_EXT bit is set in the mbuf's flags. 170 * 171 * Arguments: 172 * mb The existing mbuf to which to attach the provided buffer. 173 * buf The address of the provided external storage buffer. 174 * size The size of the provided buffer. 175 * freef A pointer to a routine that is responsible for freeing the 176 * provided external storage buffer. 177 * args A pointer to an argument structure (of any type) to be passed 178 * to the provided freef routine (may be NULL). 179 * flags Any other flags to be passed to the provided mbuf. 180 * type The type that the external storage buffer should be 181 * labeled with. 182 * 183 * Returns: 184 * Nothing. 185 */ 186void 187m_extadd(struct mbuf *mb, caddr_t buf, u_int size, 188 void (*freef)(void *, void *), void *args, int flags, int type) 189{ 190 KASSERT(type != EXT_CLUSTER, ("%s: EXT_CLUSTER not allowed", __func__)); 191 192 if (type != EXT_EXTREF) 193 mb->m_ext.ref_cnt = (u_int *)uma_zalloc(zone_ext_refcnt, M_NOWAIT); 194 if (mb->m_ext.ref_cnt != NULL) { 195 *(mb->m_ext.ref_cnt) = 1; 196 mb->m_flags |= (M_EXT | flags); 197 mb->m_ext.ext_buf = buf; 198 mb->m_data = mb->m_ext.ext_buf; 199 mb->m_ext.ext_size = size; 200 mb->m_ext.ext_free = freef; 201 mb->m_ext.ext_args = args; 202 mb->m_ext.ext_type = type; 203 } 204} 205 206/* 207 * Non-directly-exported function to clean up after mbufs with M_EXT 208 * storage attached to them if the reference count hits 1. 209 */ 210void 211mb_free_ext(struct mbuf *m) 212{ 213 KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__)); 214 KASSERT(m->m_ext.ref_cnt != NULL, ("%s: ref_cnt not set", __func__)); 215 216 /* Free attached storage if this mbuf is the only reference to it. */ 217 if (*(m->m_ext.ref_cnt) == 1 ||
| 34 35#include "opt_mac.h" 36#include "opt_param.h" 37#include "opt_mbuf_stress_test.h" 38 39#include <sys/param.h> 40#include <sys/systm.h> 41#include <sys/kernel.h> 42#include <sys/limits.h> 43#include <sys/lock.h> 44#include <sys/mac.h> 45#include <sys/malloc.h> 46#include <sys/mbuf.h> 47#include <sys/sysctl.h> 48#include <sys/domain.h> 49#include <sys/protosw.h> 50#include <sys/uio.h> 51 52int max_linkhdr; 53int max_protohdr; 54int max_hdr; 55int max_datalen; 56#ifdef MBUF_STRESS_TEST 57int m_defragpackets; 58int m_defragbytes; 59int m_defraguseless; 60int m_defragfailure; 61int m_defragrandomfailures; 62#endif 63 64/* 65 * sysctl(8) exported objects 66 */ 67SYSCTL_DECL(_kern_ipc); 68SYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW, 69 &max_linkhdr, 0, ""); 70SYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW, 71 &max_protohdr, 0, ""); 72SYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, ""); 73SYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW, 74 &max_datalen, 0, ""); 75#ifdef MBUF_STRESS_TEST 76SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragpackets, CTLFLAG_RD, 77 &m_defragpackets, 0, ""); 78SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragbytes, CTLFLAG_RD, 79 &m_defragbytes, 0, ""); 80SYSCTL_INT(_kern_ipc, OID_AUTO, m_defraguseless, CTLFLAG_RD, 81 &m_defraguseless, 0, ""); 82SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragfailure, CTLFLAG_RD, 83 &m_defragfailure, 0, ""); 84SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragrandomfailures, CTLFLAG_RW, 85 &m_defragrandomfailures, 0, ""); 86#endif 87 88/* 89 * Allocate a given length worth of mbufs and/or clusters (whatever fits 90 * best) and return a pointer to the top of the allocated chain. If an 91 * existing mbuf chain is provided, then we will append the new chain 92 * to the existing one but still return the top of the newly allocated 93 * chain. 94 */ 95struct mbuf * 96m_getm(struct mbuf *m, int len, int how, short type) 97{ 98 struct mbuf *mb, *top, *cur, *mtail; 99 int num, rem; 100 int i; 101 102 KASSERT(len >= 0, ("m_getm(): len is < 0")); 103 104 /* If m != NULL, we will append to the end of that chain. */ 105 if (m != NULL) 106 for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next); 107 else 108 mtail = NULL; 109 110 /* 111 * Calculate how many mbufs+clusters ("packets") we need and how much 112 * leftover there is after that and allocate the first mbuf+cluster 113 * if required. 114 */ 115 num = len / MCLBYTES; 116 rem = len % MCLBYTES; 117 top = cur = NULL; 118 if (num > 0) { 119 if ((top = cur = m_getcl(how, type, 0)) == NULL) 120 goto failed; 121 top->m_len = 0; 122 } 123 num--; 124 125 for (i = 0; i < num; i++) { 126 mb = m_getcl(how, type, 0); 127 if (mb == NULL) 128 goto failed; 129 mb->m_len = 0; 130 cur = (cur->m_next = mb); 131 } 132 if (rem > 0) { 133 mb = (rem > MINCLSIZE) ? 134 m_getcl(how, type, 0) : m_get(how, type); 135 if (mb == NULL) 136 goto failed; 137 mb->m_len = 0; 138 if (cur == NULL) 139 top = mb; 140 else 141 cur->m_next = mb; 142 } 143 144 if (mtail != NULL) 145 mtail->m_next = top; 146 return top; 147failed: 148 if (top != NULL) 149 m_freem(top); 150 return NULL; 151} 152 153/* 154 * Free an entire chain of mbufs and associated external buffers, if 155 * applicable. 156 */ 157void 158m_freem(struct mbuf *mb) 159{ 160 161 while (mb != NULL) 162 mb = m_free(mb); 163} 164 165/*- 166 * Configure a provided mbuf to refer to the provided external storage 167 * buffer and setup a reference count for said buffer. If the setting 168 * up of the reference count fails, the M_EXT bit will not be set. If 169 * successfull, the M_EXT bit is set in the mbuf's flags. 170 * 171 * Arguments: 172 * mb The existing mbuf to which to attach the provided buffer. 173 * buf The address of the provided external storage buffer. 174 * size The size of the provided buffer. 175 * freef A pointer to a routine that is responsible for freeing the 176 * provided external storage buffer. 177 * args A pointer to an argument structure (of any type) to be passed 178 * to the provided freef routine (may be NULL). 179 * flags Any other flags to be passed to the provided mbuf. 180 * type The type that the external storage buffer should be 181 * labeled with. 182 * 183 * Returns: 184 * Nothing. 185 */ 186void 187m_extadd(struct mbuf *mb, caddr_t buf, u_int size, 188 void (*freef)(void *, void *), void *args, int flags, int type) 189{ 190 KASSERT(type != EXT_CLUSTER, ("%s: EXT_CLUSTER not allowed", __func__)); 191 192 if (type != EXT_EXTREF) 193 mb->m_ext.ref_cnt = (u_int *)uma_zalloc(zone_ext_refcnt, M_NOWAIT); 194 if (mb->m_ext.ref_cnt != NULL) { 195 *(mb->m_ext.ref_cnt) = 1; 196 mb->m_flags |= (M_EXT | flags); 197 mb->m_ext.ext_buf = buf; 198 mb->m_data = mb->m_ext.ext_buf; 199 mb->m_ext.ext_size = size; 200 mb->m_ext.ext_free = freef; 201 mb->m_ext.ext_args = args; 202 mb->m_ext.ext_type = type; 203 } 204} 205 206/* 207 * Non-directly-exported function to clean up after mbufs with M_EXT 208 * storage attached to them if the reference count hits 1. 209 */ 210void 211mb_free_ext(struct mbuf *m) 212{ 213 KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__)); 214 KASSERT(m->m_ext.ref_cnt != NULL, ("%s: ref_cnt not set", __func__)); 215 216 /* Free attached storage if this mbuf is the only reference to it. */ 217 if (*(m->m_ext.ref_cnt) == 1 ||
|
221 uma_zfree(zone_pack, m); 222 return; /* Job done. */ 223 break; 224 case EXT_JUMBO9: 225 uma_zfree(zone_jumbo9, m->m_ext.ext_buf); 226 break; 227 case EXT_JUMBO16: 228 uma_zfree(zone_jumbo16, m->m_ext.ext_buf); 229 break; 230 case EXT_SFBUF: 231 case EXT_NET_DRV: 232 case EXT_MOD_TYPE: 233 case EXT_DISPOSABLE: 234 *(m->m_ext.ref_cnt) = 0; 235 uma_zfree(zone_ext_refcnt, __DEVOLATILE(u_int *, 236 m->m_ext.ref_cnt)); 237 /* FALLTHROUGH */ 238 case EXT_EXTREF: 239 KASSERT(m->m_ext.ext_free != NULL, 240 ("%s: ext_free not set", __func__)); 241 (*(m->m_ext.ext_free))(m->m_ext.ext_buf, 242 m->m_ext.ext_args); 243 break; 244 default: 245 KASSERT(m->m_ext.ext_type == 0, 246 ("%s: unknown ext_type", __func__)); 247 } 248 } 249 /* 250 * Free this mbuf back to the mbuf zone with all m_ext 251 * information purged. 252 */ 253 m->m_ext.ext_buf = NULL; 254 m->m_ext.ext_free = NULL; 255 m->m_ext.ext_args = NULL; 256 m->m_ext.ref_cnt = NULL; 257 m->m_ext.ext_size = 0; 258 m->m_ext.ext_type = 0; 259 m->m_flags &= ~M_EXT; 260 uma_zfree(zone_mbuf, m); 261} 262 263/* 264 * Attach the the cluster from *m to *n, set up m_ext in *n 265 * and bump the refcount of the cluster. 266 */ 267static void 268mb_dupcl(struct mbuf *n, struct mbuf *m) 269{ 270 KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__)); 271 KASSERT(m->m_ext.ref_cnt != NULL, ("%s: ref_cnt not set", __func__)); 272 KASSERT((n->m_flags & M_EXT) == 0, ("%s: M_EXT set", __func__)); 273 274 if (*(m->m_ext.ref_cnt) == 1) 275 *(m->m_ext.ref_cnt) += 1; 276 else 277 atomic_add_int(m->m_ext.ref_cnt, 1); 278 n->m_ext.ext_buf = m->m_ext.ext_buf; 279 n->m_ext.ext_free = m->m_ext.ext_free; 280 n->m_ext.ext_args = m->m_ext.ext_args; 281 n->m_ext.ext_size = m->m_ext.ext_size; 282 n->m_ext.ref_cnt = m->m_ext.ref_cnt; 283 n->m_ext.ext_type = m->m_ext.ext_type; 284 n->m_flags |= M_EXT; 285} 286 287/* 288 * Clean up mbuf (chain) from any tags and packet headers. 289 * If "all" is set then the first mbuf in the chain will be 290 * cleaned too. 291 */ 292void 293m_demote(struct mbuf *m0, int all) 294{ 295 struct mbuf *m; 296 297 for (m = all ? m0 : m0->m_next; m != NULL; m = m->m_next) { 298 if (m->m_flags & M_PKTHDR) { 299 m_tag_delete_chain(m, NULL); 300 m->m_flags &= ~M_PKTHDR; 301 bzero(&m->m_pkthdr, sizeof(struct pkthdr)); 302 } 303 if (m->m_type == MT_HEADER) 304 m->m_type = MT_DATA; 305 if (m != m0 && m->m_nextpkt != NULL) 306 m->m_nextpkt = NULL; 307 m->m_flags = m->m_flags & (M_EXT|M_EOR|M_RDONLY|M_FREELIST); 308 } 309} 310 311/* 312 * Sanity checks on mbuf (chain) for use in KASSERT() and general 313 * debugging. 314 * Returns 0 or panics when bad and 1 on all tests passed. 315 * Sanitize, 0 to run M_SANITY_ACTION, 1 to garble things so they 316 * blow up later. 317 */ 318int 319m_sanity(struct mbuf *m0, int sanitize) 320{ 321 struct mbuf *m; 322 caddr_t a, b; 323 int pktlen = 0; 324 325#define M_SANITY_ACTION(s) return (0) 326/* #define M_SANITY_ACTION(s) panic("mbuf %p: " s, m) */ 327 328 for (m = m0; m != NULL; m = m->m_next) { 329 /* 330 * Basic pointer checks. If any of these fails then some 331 * unrelated kernel memory before or after us is trashed. 332 * No way to recover from that. 333 */ 334 a = ((m->m_flags & M_EXT) ? m->m_ext.ext_buf : 335 ((m->m_flags & M_PKTHDR) ? (caddr_t)(&m->m_pktdat) : 336 (caddr_t)(&m->m_dat)) ); 337 b = (caddr_t)(a + (m->m_flags & M_EXT ? m->m_ext.ext_size : 338 ((m->m_flags & M_PKTHDR) ? MHLEN : MLEN))); 339 if ((caddr_t)m->m_data < a) 340 M_SANITY_ACTION("m_data outside mbuf data range left"); 341 if ((caddr_t)m->m_data > b) 342 M_SANITY_ACTION("m_data outside mbuf data range right"); 343 if ((caddr_t)m->m_data + m->m_len > b) 344 M_SANITY_ACTION("m_data + m_len exeeds mbuf space"); 345 if ((m->m_flags & M_PKTHDR) && m->m_pkthdr.header) { 346 if ((caddr_t)m->m_pkthdr.header < a || 347 (caddr_t)m->m_pkthdr.header > b) 348 M_SANITY_ACTION("m_pkthdr.header outside mbuf data range"); 349 } 350 351 /* m->m_nextpkt may only be set on first mbuf in chain. */ 352 if (m != m0 && m->m_nextpkt != NULL) { 353 if (sanitize) { 354 m_freem(m->m_nextpkt); 355 m->m_nextpkt = (struct mbuf *)0xDEADC0DE; 356 } else 357 M_SANITY_ACTION("m->m_nextpkt on in-chain mbuf"); 358 } 359 360 /* correct type correlations. */ 361 if (m->m_type == MT_HEADER && !(m->m_flags & M_PKTHDR)) { 362 if (sanitize) 363 m->m_type = MT_DATA; 364 else 365 M_SANITY_ACTION("MT_HEADER set but not M_PKTHDR"); 366 } 367 368 /* packet length (not mbuf length!) calculation */ 369 if (m0->m_flags & M_PKTHDR) 370 pktlen += m->m_len; 371 372 /* m_tags may only be attached to first mbuf in chain. */ 373 if (m != m0 && m->m_flags & M_PKTHDR && 374 !SLIST_EMPTY(&m->m_pkthdr.tags)) { 375 if (sanitize) { 376 m_tag_delete_chain(m, NULL); 377 /* put in 0xDEADC0DE perhaps? */ 378 } else 379 M_SANITY_ACTION("m_tags on in-chain mbuf"); 380 } 381 382 /* M_PKTHDR may only be set on first mbuf in chain */ 383 if (m != m0 && m->m_flags & M_PKTHDR) { 384 if (sanitize) { 385 bzero(&m->m_pkthdr, sizeof(m->m_pkthdr)); 386 m->m_flags &= ~M_PKTHDR; 387 /* put in 0xDEADCODE and leave hdr flag in */ 388 } else 389 M_SANITY_ACTION("M_PKTHDR on in-chain mbuf"); 390 } 391 } 392 m = m0; 393 if (pktlen && pktlen != m->m_pkthdr.len) { 394 if (sanitize) 395 m->m_pkthdr.len = 0; 396 else 397 M_SANITY_ACTION("m_pkthdr.len != mbuf chain length"); 398 } 399 return 1; 400 401#undef M_SANITY_ACTION 402} 403 404 405/* 406 * "Move" mbuf pkthdr from "from" to "to". 407 * "from" must have M_PKTHDR set, and "to" must be empty. 408 */ 409void 410m_move_pkthdr(struct mbuf *to, struct mbuf *from) 411{ 412 413#if 0 414 /* see below for why these are not enabled */ 415 M_ASSERTPKTHDR(to); 416 /* Note: with MAC, this may not be a good assertion. */ 417 KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), 418 ("m_move_pkthdr: to has tags")); 419#endif 420#ifdef MAC 421 /* 422 * XXXMAC: It could be this should also occur for non-MAC? 423 */ 424 if (to->m_flags & M_PKTHDR) 425 m_tag_delete_chain(to, NULL); 426#endif 427 to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT); 428 if ((to->m_flags & M_EXT) == 0) 429 to->m_data = to->m_pktdat; 430 to->m_pkthdr = from->m_pkthdr; /* especially tags */ 431 SLIST_INIT(&from->m_pkthdr.tags); /* purge tags from src */ 432 from->m_flags &= ~M_PKTHDR; 433} 434 435/* 436 * Duplicate "from"'s mbuf pkthdr in "to". 437 * "from" must have M_PKTHDR set, and "to" must be empty. 438 * In particular, this does a deep copy of the packet tags. 439 */ 440int 441m_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how) 442{ 443 444#if 0 445 /* 446 * The mbuf allocator only initializes the pkthdr 447 * when the mbuf is allocated with MGETHDR. Many users 448 * (e.g. m_copy*, m_prepend) use MGET and then 449 * smash the pkthdr as needed causing these 450 * assertions to trip. For now just disable them. 451 */ 452 M_ASSERTPKTHDR(to); 453 /* Note: with MAC, this may not be a good assertion. */ 454 KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), ("m_dup_pkthdr: to has tags")); 455#endif 456 MBUF_CHECKSLEEP(how); 457#ifdef MAC 458 if (to->m_flags & M_PKTHDR) 459 m_tag_delete_chain(to, NULL); 460#endif 461 to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT); 462 if ((to->m_flags & M_EXT) == 0) 463 to->m_data = to->m_pktdat; 464 to->m_pkthdr = from->m_pkthdr; 465 SLIST_INIT(&to->m_pkthdr.tags); 466 return (m_tag_copy_chain(to, from, MBTOM(how))); 467} 468 469/* 470 * Lesser-used path for M_PREPEND: 471 * allocate new mbuf to prepend to chain, 472 * copy junk along. 473 */ 474struct mbuf * 475m_prepend(struct mbuf *m, int len, int how) 476{ 477 struct mbuf *mn; 478 479 if (m->m_flags & M_PKTHDR) 480 MGETHDR(mn, how, m->m_type); 481 else 482 MGET(mn, how, m->m_type); 483 if (mn == NULL) { 484 m_freem(m); 485 return (NULL); 486 } 487 if (m->m_flags & M_PKTHDR) 488 M_MOVE_PKTHDR(mn, m); 489 mn->m_next = m; 490 m = mn; 491 if (len < MHLEN) 492 MH_ALIGN(m, len); 493 m->m_len = len; 494 return (m); 495} 496 497/* 498 * Make a copy of an mbuf chain starting "off0" bytes from the beginning, 499 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf. 500 * The wait parameter is a choice of M_TRYWAIT/M_DONTWAIT from caller. 501 * Note that the copy is read-only, because clusters are not copied, 502 * only their reference counts are incremented. 503 */ 504struct mbuf * 505m_copym(struct mbuf *m, int off0, int len, int wait) 506{ 507 struct mbuf *n, **np; 508 int off = off0; 509 struct mbuf *top; 510 int copyhdr = 0; 511 512 KASSERT(off >= 0, ("m_copym, negative off %d", off)); 513 KASSERT(len >= 0, ("m_copym, negative len %d", len)); 514 MBUF_CHECKSLEEP(wait); 515 if (off == 0 && m->m_flags & M_PKTHDR) 516 copyhdr = 1; 517 while (off > 0) { 518 KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain")); 519 if (off < m->m_len) 520 break; 521 off -= m->m_len; 522 m = m->m_next; 523 } 524 np = ⊤ 525 top = 0; 526 while (len > 0) { 527 if (m == NULL) { 528 KASSERT(len == M_COPYALL, 529 ("m_copym, length > size of mbuf chain")); 530 break; 531 } 532 if (copyhdr) 533 MGETHDR(n, wait, m->m_type); 534 else 535 MGET(n, wait, m->m_type); 536 *np = n; 537 if (n == NULL) 538 goto nospace; 539 if (copyhdr) { 540 if (!m_dup_pkthdr(n, m, wait)) 541 goto nospace; 542 if (len == M_COPYALL) 543 n->m_pkthdr.len -= off0; 544 else 545 n->m_pkthdr.len = len; 546 copyhdr = 0; 547 } 548 n->m_len = min(len, m->m_len - off); 549 if (m->m_flags & M_EXT) { 550 n->m_data = m->m_data + off; 551 mb_dupcl(n, m); 552 } else 553 bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), 554 (u_int)n->m_len); 555 if (len != M_COPYALL) 556 len -= n->m_len; 557 off = 0; 558 m = m->m_next; 559 np = &n->m_next; 560 } 561 if (top == NULL) 562 mbstat.m_mcfail++; /* XXX: No consistency. */ 563 564 return (top); 565nospace: 566 m_freem(top); 567 mbstat.m_mcfail++; /* XXX: No consistency. */ 568 return (NULL); 569} 570 571/* 572 * Returns mbuf chain with new head for the prepending case. 573 * Copies from mbuf (chain) n from off for len to mbuf (chain) m 574 * either prepending or appending the data. 575 * The resulting mbuf (chain) m is fully writeable. 576 * m is destination (is made writeable) 577 * n is source, off is offset in source, len is len from offset 578 * dir, 0 append, 1 prepend 579 * how, wait or nowait 580 */ 581 582static int 583m_bcopyxxx(void *s, void *t, u_int len) 584{ 585 bcopy(s, t, (size_t)len); 586 return 0; 587} 588 589struct mbuf * 590m_copymdata(struct mbuf *m, struct mbuf *n, int off, int len, 591 int prep, int how) 592{ 593 struct mbuf *mm, *x, *z, *prev = NULL; 594 caddr_t p; 595 int i, nlen = 0; 596 caddr_t buf[MLEN]; 597 598 KASSERT(m != NULL && n != NULL, ("m_copymdata, no target or source")); 599 KASSERT(off >= 0, ("m_copymdata, negative off %d", off)); 600 KASSERT(len >= 0, ("m_copymdata, negative len %d", len)); 601 KASSERT(prep == 0 || prep == 1, ("m_copymdata, unknown direction %d", prep)); 602 603 mm = m; 604 if (!prep) { 605 while(mm->m_next) { 606 prev = mm; 607 mm = mm->m_next; 608 } 609 } 610 for (z = n; z != NULL; z = z->m_next) 611 nlen += z->m_len; 612 if (len == M_COPYALL) 613 len = nlen - off; 614 if (off + len > nlen || len < 1) 615 return NULL; 616 617 if (!M_WRITABLE(mm)) { 618 /* XXX: Use proper m_xxx function instead. */ 619 x = m_getcl(how, MT_DATA, mm->m_flags); 620 if (x == NULL) 621 return NULL; 622 bcopy(mm->m_ext.ext_buf, x->m_ext.ext_buf, x->m_ext.ext_size); 623 p = x->m_ext.ext_buf + (mm->m_data - mm->m_ext.ext_buf); 624 x->m_data = p; 625 mm->m_next = NULL; 626 if (mm != m) 627 prev->m_next = x; 628 m_free(mm); 629 mm = x; 630 } 631 632 /* 633 * Append/prepend the data. Allocating mbufs as necessary. 634 */ 635 /* Shortcut if enough free space in first/last mbuf. */ 636 if (!prep && M_TRAILINGSPACE(mm) >= len) { 637 m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t) + 638 mm->m_len); 639 mm->m_len += len; 640 mm->m_pkthdr.len += len; 641 return m; 642 } 643 if (prep && M_LEADINGSPACE(mm) >= len) { 644 mm->m_data = mtod(mm, caddr_t) - len; 645 m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t)); 646 mm->m_len += len; 647 mm->m_pkthdr.len += len; 648 return mm; 649 } 650 651 /* Expand first/last mbuf to cluster if possible. */ 652 if (!prep && !(mm->m_flags & M_EXT) && len > M_TRAILINGSPACE(mm)) { 653 bcopy(mm->m_data, &buf, mm->m_len); 654 m_clget(mm, how); 655 if (!(mm->m_flags & M_EXT)) 656 return NULL; 657 bcopy(&buf, mm->m_ext.ext_buf, mm->m_len); 658 mm->m_data = mm->m_ext.ext_buf; 659 mm->m_pkthdr.header = NULL; 660 } 661 if (prep && !(mm->m_flags & M_EXT) && len > M_LEADINGSPACE(mm)) { 662 bcopy(mm->m_data, &buf, mm->m_len); 663 m_clget(mm, how); 664 if (!(mm->m_flags & M_EXT)) 665 return NULL; 666 bcopy(&buf, (caddr_t *)mm->m_ext.ext_buf + 667 mm->m_ext.ext_size - mm->m_len, mm->m_len); 668 mm->m_data = (caddr_t)mm->m_ext.ext_buf + 669 mm->m_ext.ext_size - mm->m_len; 670 mm->m_pkthdr.header = NULL; 671 } 672 673 /* Append/prepend as many mbuf (clusters) as necessary to fit len. */ 674 if (!prep && len > M_TRAILINGSPACE(mm)) { 675 if (!m_getm(mm, len - M_TRAILINGSPACE(mm), how, MT_DATA)) 676 return NULL; 677 } 678 if (prep && len > M_LEADINGSPACE(mm)) { 679 if (!(z = m_getm(NULL, len - M_LEADINGSPACE(mm), how, MT_DATA))) 680 return NULL; 681 i = 0; 682 for (x = z; x != NULL; x = x->m_next) { 683 i += x->m_flags & M_EXT ? x->m_ext.ext_size : 684 (x->m_flags & M_PKTHDR ? MHLEN : MLEN); 685 if (!x->m_next) 686 break; 687 } 688 z->m_data += i - len; 689 m_move_pkthdr(mm, z); 690 x->m_next = mm; 691 mm = z; 692 } 693 694 /* Seek to start position in source mbuf. Optimization for long chains. */ 695 while (off > 0) { 696 if (off < n->m_len) 697 break; 698 off -= n->m_len; 699 n = n->m_next; 700 } 701 702 /* Copy data into target mbuf. */ 703 z = mm; 704 while (len > 0) { 705 KASSERT(z != NULL, ("m_copymdata, falling off target edge")); 706 i = M_TRAILINGSPACE(z); 707 m_apply(n, off, i, m_bcopyxxx, mtod(z, caddr_t) + z->m_len); 708 z->m_len += i; 709 /* fixup pkthdr.len if necessary */ 710 if ((prep ? mm : m)->m_flags & M_PKTHDR) 711 (prep ? mm : m)->m_pkthdr.len += i; 712 off += i; 713 len -= i; 714 z = z->m_next; 715 } 716 return (prep ? mm : m); 717} 718 719/* 720 * Copy an entire packet, including header (which must be present). 721 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'. 722 * Note that the copy is read-only, because clusters are not copied, 723 * only their reference counts are incremented. 724 * Preserve alignment of the first mbuf so if the creator has left 725 * some room at the beginning (e.g. for inserting protocol headers) 726 * the copies still have the room available. 727 */ 728struct mbuf * 729m_copypacket(struct mbuf *m, int how) 730{ 731 struct mbuf *top, *n, *o; 732 733 MBUF_CHECKSLEEP(how); 734 MGET(n, how, m->m_type); 735 top = n; 736 if (n == NULL) 737 goto nospace; 738 739 if (!m_dup_pkthdr(n, m, how)) 740 goto nospace; 741 n->m_len = m->m_len; 742 if (m->m_flags & M_EXT) { 743 n->m_data = m->m_data; 744 mb_dupcl(n, m); 745 } else { 746 n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat ); 747 bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 748 } 749 750 m = m->m_next; 751 while (m) { 752 MGET(o, how, m->m_type); 753 if (o == NULL) 754 goto nospace; 755 756 n->m_next = o; 757 n = n->m_next; 758 759 n->m_len = m->m_len; 760 if (m->m_flags & M_EXT) { 761 n->m_data = m->m_data; 762 mb_dupcl(n, m); 763 } else { 764 bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 765 } 766 767 m = m->m_next; 768 } 769 return top; 770nospace: 771 m_freem(top); 772 mbstat.m_mcfail++; /* XXX: No consistency. */ 773 return (NULL); 774} 775 776/* 777 * Copy data from an mbuf chain starting "off" bytes from the beginning, 778 * continuing for "len" bytes, into the indicated buffer. 779 */ 780void 781m_copydata(const struct mbuf *m, int off, int len, caddr_t cp) 782{ 783 u_int count; 784 785 KASSERT(off >= 0, ("m_copydata, negative off %d", off)); 786 KASSERT(len >= 0, ("m_copydata, negative len %d", len)); 787 while (off > 0) { 788 KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain")); 789 if (off < m->m_len) 790 break; 791 off -= m->m_len; 792 m = m->m_next; 793 } 794 while (len > 0) { 795 KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain")); 796 count = min(m->m_len - off, len); 797 bcopy(mtod(m, caddr_t) + off, cp, count); 798 len -= count; 799 cp += count; 800 off = 0; 801 m = m->m_next; 802 } 803} 804 805/* 806 * Copy a packet header mbuf chain into a completely new chain, including 807 * copying any mbuf clusters. Use this instead of m_copypacket() when 808 * you need a writable copy of an mbuf chain. 809 */ 810struct mbuf * 811m_dup(struct mbuf *m, int how) 812{ 813 struct mbuf **p, *top = NULL; 814 int remain, moff, nsize; 815 816 MBUF_CHECKSLEEP(how); 817 /* Sanity check */ 818 if (m == NULL) 819 return (NULL); 820 M_ASSERTPKTHDR(m); 821 822 /* While there's more data, get a new mbuf, tack it on, and fill it */ 823 remain = m->m_pkthdr.len; 824 moff = 0; 825 p = ⊤ 826 while (remain > 0 || top == NULL) { /* allow m->m_pkthdr.len == 0 */ 827 struct mbuf *n; 828 829 /* Get the next new mbuf */ 830 if (remain >= MINCLSIZE) { 831 n = m_getcl(how, m->m_type, 0); 832 nsize = MCLBYTES; 833 } else { 834 n = m_get(how, m->m_type); 835 nsize = MLEN; 836 } 837 if (n == NULL) 838 goto nospace; 839 840 if (top == NULL) { /* First one, must be PKTHDR */ 841 if (!m_dup_pkthdr(n, m, how)) { 842 m_free(n); 843 goto nospace; 844 } 845 nsize = MHLEN; 846 } 847 n->m_len = 0; 848 849 /* Link it into the new chain */ 850 *p = n; 851 p = &n->m_next; 852 853 /* Copy data from original mbuf(s) into new mbuf */ 854 while (n->m_len < nsize && m != NULL) { 855 int chunk = min(nsize - n->m_len, m->m_len - moff); 856 857 bcopy(m->m_data + moff, n->m_data + n->m_len, chunk); 858 moff += chunk; 859 n->m_len += chunk; 860 remain -= chunk; 861 if (moff == m->m_len) { 862 m = m->m_next; 863 moff = 0; 864 } 865 } 866 867 /* Check correct total mbuf length */ 868 KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL), 869 ("%s: bogus m_pkthdr.len", __func__)); 870 } 871 return (top); 872 873nospace: 874 m_freem(top); 875 mbstat.m_mcfail++; /* XXX: No consistency. */ 876 return (NULL); 877} 878 879/* 880 * Concatenate mbuf chain n to m. 881 * Both chains must be of the same type (e.g. MT_DATA). 882 * Any m_pkthdr is not updated. 883 */ 884void 885m_cat(struct mbuf *m, struct mbuf *n) 886{ 887 while (m->m_next) 888 m = m->m_next; 889 while (n) { 890 if (m->m_flags & M_EXT || 891 m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) { 892 /* just join the two chains */ 893 m->m_next = n; 894 return; 895 } 896 /* splat the data from one into the other */ 897 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 898 (u_int)n->m_len); 899 m->m_len += n->m_len; 900 n = m_free(n); 901 } 902} 903 904void 905m_adj(struct mbuf *mp, int req_len) 906{ 907 int len = req_len; 908 struct mbuf *m; 909 int count; 910 911 if ((m = mp) == NULL) 912 return; 913 if (len >= 0) { 914 /* 915 * Trim from head. 916 */ 917 while (m != NULL && len > 0) { 918 if (m->m_len <= len) { 919 len -= m->m_len; 920 m->m_len = 0; 921 m = m->m_next; 922 } else { 923 m->m_len -= len; 924 m->m_data += len; 925 len = 0; 926 } 927 } 928 m = mp; 929 if (mp->m_flags & M_PKTHDR) 930 m->m_pkthdr.len -= (req_len - len); 931 } else { 932 /* 933 * Trim from tail. Scan the mbuf chain, 934 * calculating its length and finding the last mbuf. 935 * If the adjustment only affects this mbuf, then just 936 * adjust and return. Otherwise, rescan and truncate 937 * after the remaining size. 938 */ 939 len = -len; 940 count = 0; 941 for (;;) { 942 count += m->m_len; 943 if (m->m_next == (struct mbuf *)0) 944 break; 945 m = m->m_next; 946 } 947 if (m->m_len >= len) { 948 m->m_len -= len; 949 if (mp->m_flags & M_PKTHDR) 950 mp->m_pkthdr.len -= len; 951 return; 952 } 953 count -= len; 954 if (count < 0) 955 count = 0; 956 /* 957 * Correct length for chain is "count". 958 * Find the mbuf with last data, adjust its length, 959 * and toss data from remaining mbufs on chain. 960 */ 961 m = mp; 962 if (m->m_flags & M_PKTHDR) 963 m->m_pkthdr.len = count; 964 for (; m; m = m->m_next) { 965 if (m->m_len >= count) { 966 m->m_len = count; 967 if (m->m_next != NULL) { 968 m_freem(m->m_next); 969 m->m_next = NULL; 970 } 971 break; 972 } 973 count -= m->m_len; 974 } 975 } 976} 977 978/* 979 * Rearange an mbuf chain so that len bytes are contiguous 980 * and in the data area of an mbuf (so that mtod and dtom 981 * will work for a structure of size len). Returns the resulting 982 * mbuf chain on success, frees it and returns null on failure. 983 * If there is room, it will add up to max_protohdr-len extra bytes to the 984 * contiguous region in an attempt to avoid being called next time. 985 */ 986struct mbuf * 987m_pullup(struct mbuf *n, int len) 988{ 989 struct mbuf *m; 990 int count; 991 int space; 992 993 /* 994 * If first mbuf has no cluster, and has room for len bytes 995 * without shifting current data, pullup into it, 996 * otherwise allocate a new mbuf to prepend to the chain. 997 */ 998 if ((n->m_flags & M_EXT) == 0 && 999 n->m_data + len < &n->m_dat[MLEN] && n->m_next) { 1000 if (n->m_len >= len) 1001 return (n); 1002 m = n; 1003 n = n->m_next; 1004 len -= m->m_len; 1005 } else { 1006 if (len > MHLEN) 1007 goto bad; 1008 MGET(m, M_DONTWAIT, n->m_type); 1009 if (m == NULL) 1010 goto bad; 1011 m->m_len = 0; 1012 if (n->m_flags & M_PKTHDR) 1013 M_MOVE_PKTHDR(m, n); 1014 } 1015 space = &m->m_dat[MLEN] - (m->m_data + m->m_len); 1016 do { 1017 count = min(min(max(len, max_protohdr), space), n->m_len); 1018 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 1019 (u_int)count); 1020 len -= count; 1021 m->m_len += count; 1022 n->m_len -= count; 1023 space -= count; 1024 if (n->m_len) 1025 n->m_data += count; 1026 else 1027 n = m_free(n); 1028 } while (len > 0 && n); 1029 if (len > 0) { 1030 (void) m_free(m); 1031 goto bad; 1032 } 1033 m->m_next = n; 1034 return (m); 1035bad: 1036 m_freem(n); 1037 mbstat.m_mpfail++; /* XXX: No consistency. */ 1038 return (NULL); 1039} 1040 1041/* 1042 * Like m_pullup(), except a new mbuf is always allocated, and we allow 1043 * the amount of empty space before the data in the new mbuf to be specified 1044 * (in the event that the caller expects to prepend later). 1045 */ 1046int MSFail; 1047 1048struct mbuf * 1049m_copyup(struct mbuf *n, int len, int dstoff) 1050{ 1051 struct mbuf *m; 1052 int count, space; 1053 1054 if (len > (MHLEN - dstoff)) 1055 goto bad; 1056 MGET(m, M_DONTWAIT, n->m_type); 1057 if (m == NULL) 1058 goto bad; 1059 m->m_len = 0; 1060 if (n->m_flags & M_PKTHDR) 1061 M_MOVE_PKTHDR(m, n); 1062 m->m_data += dstoff; 1063 space = &m->m_dat[MLEN] - (m->m_data + m->m_len); 1064 do { 1065 count = min(min(max(len, max_protohdr), space), n->m_len); 1066 memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t), 1067 (unsigned)count); 1068 len -= count; 1069 m->m_len += count; 1070 n->m_len -= count; 1071 space -= count; 1072 if (n->m_len) 1073 n->m_data += count; 1074 else 1075 n = m_free(n); 1076 } while (len > 0 && n); 1077 if (len > 0) { 1078 (void) m_free(m); 1079 goto bad; 1080 } 1081 m->m_next = n; 1082 return (m); 1083 bad: 1084 m_freem(n); 1085 MSFail++; 1086 return (NULL); 1087} 1088 1089/* 1090 * Partition an mbuf chain in two pieces, returning the tail -- 1091 * all but the first len0 bytes. In case of failure, it returns NULL and 1092 * attempts to restore the chain to its original state. 1093 * 1094 * Note that the resulting mbufs might be read-only, because the new 1095 * mbuf can end up sharing an mbuf cluster with the original mbuf if 1096 * the "breaking point" happens to lie within a cluster mbuf. Use the 1097 * M_WRITABLE() macro to check for this case. 1098 */ 1099struct mbuf * 1100m_split(struct mbuf *m0, int len0, int wait) 1101{ 1102 struct mbuf *m, *n; 1103 u_int len = len0, remain; 1104 1105 MBUF_CHECKSLEEP(wait); 1106 for (m = m0; m && len > m->m_len; m = m->m_next) 1107 len -= m->m_len; 1108 if (m == NULL) 1109 return (NULL); 1110 remain = m->m_len - len; 1111 if (m0->m_flags & M_PKTHDR) { 1112 MGETHDR(n, wait, m0->m_type); 1113 if (n == NULL) 1114 return (NULL); 1115 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif; 1116 n->m_pkthdr.len = m0->m_pkthdr.len - len0; 1117 m0->m_pkthdr.len = len0; 1118 if (m->m_flags & M_EXT) 1119 goto extpacket; 1120 if (remain > MHLEN) { 1121 /* m can't be the lead packet */ 1122 MH_ALIGN(n, 0); 1123 n->m_next = m_split(m, len, wait); 1124 if (n->m_next == NULL) { 1125 (void) m_free(n); 1126 return (NULL); 1127 } else { 1128 n->m_len = 0; 1129 return (n); 1130 } 1131 } else 1132 MH_ALIGN(n, remain); 1133 } else if (remain == 0) { 1134 n = m->m_next; 1135 m->m_next = NULL; 1136 return (n); 1137 } else { 1138 MGET(n, wait, m->m_type); 1139 if (n == NULL) 1140 return (NULL); 1141 M_ALIGN(n, remain); 1142 } 1143extpacket: 1144 if (m->m_flags & M_EXT) { 1145 n->m_data = m->m_data + len; 1146 mb_dupcl(n, m); 1147 } else { 1148 bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain); 1149 } 1150 n->m_len = remain; 1151 m->m_len = len; 1152 n->m_next = m->m_next; 1153 m->m_next = NULL; 1154 return (n); 1155} 1156/* 1157 * Routine to copy from device local memory into mbufs. 1158 * Note that `off' argument is offset into first mbuf of target chain from 1159 * which to begin copying the data to. 1160 */ 1161struct mbuf * 1162m_devget(char *buf, int totlen, int off, struct ifnet *ifp, 1163 void (*copy)(char *from, caddr_t to, u_int len)) 1164{ 1165 struct mbuf *m; 1166 struct mbuf *top = NULL, **mp = ⊤ 1167 int len; 1168 1169 if (off < 0 || off > MHLEN) 1170 return (NULL); 1171 1172 while (totlen > 0) { 1173 if (top == NULL) { /* First one, must be PKTHDR */ 1174 if (totlen + off >= MINCLSIZE) { 1175 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1176 len = MCLBYTES; 1177 } else { 1178 m = m_gethdr(M_DONTWAIT, MT_DATA); 1179 len = MHLEN; 1180 1181 /* Place initial small packet/header at end of mbuf */ 1182 if (m && totlen + off + max_linkhdr <= MLEN) { 1183 m->m_data += max_linkhdr; 1184 len -= max_linkhdr; 1185 } 1186 } 1187 if (m == NULL) 1188 return NULL; 1189 m->m_pkthdr.rcvif = ifp; 1190 m->m_pkthdr.len = totlen; 1191 } else { 1192 if (totlen + off >= MINCLSIZE) { 1193 m = m_getcl(M_DONTWAIT, MT_DATA, 0); 1194 len = MCLBYTES; 1195 } else { 1196 m = m_get(M_DONTWAIT, MT_DATA); 1197 len = MLEN; 1198 } 1199 if (m == NULL) { 1200 m_freem(top); 1201 return NULL; 1202 } 1203 } 1204 if (off) { 1205 m->m_data += off; 1206 len -= off; 1207 off = 0; 1208 } 1209 m->m_len = len = min(totlen, len); 1210 if (copy) 1211 copy(buf, mtod(m, caddr_t), (u_int)len); 1212 else 1213 bcopy(buf, mtod(m, caddr_t), (u_int)len); 1214 buf += len; 1215 *mp = m; 1216 mp = &m->m_next; 1217 totlen -= len; 1218 } 1219 return (top); 1220} 1221 1222/* 1223 * Copy data from a buffer back into the indicated mbuf chain, 1224 * starting "off" bytes from the beginning, extending the mbuf 1225 * chain if necessary. 1226 */ 1227void 1228m_copyback(struct mbuf *m0, int off, int len, c_caddr_t cp) 1229{ 1230 int mlen; 1231 struct mbuf *m = m0, *n; 1232 int totlen = 0; 1233 1234 if (m0 == NULL) 1235 return; 1236 while (off > (mlen = m->m_len)) { 1237 off -= mlen; 1238 totlen += mlen; 1239 if (m->m_next == NULL) { 1240 n = m_get(M_DONTWAIT, m->m_type); 1241 if (n == NULL) 1242 goto out; 1243 bzero(mtod(n, caddr_t), MLEN); 1244 n->m_len = min(MLEN, len + off); 1245 m->m_next = n; 1246 } 1247 m = m->m_next; 1248 } 1249 while (len > 0) { 1250 mlen = min (m->m_len - off, len); 1251 bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen); 1252 cp += mlen; 1253 len -= mlen; 1254 mlen += off; 1255 off = 0; 1256 totlen += mlen; 1257 if (len == 0) 1258 break; 1259 if (m->m_next == NULL) { 1260 n = m_get(M_DONTWAIT, m->m_type); 1261 if (n == NULL) 1262 break; 1263 n->m_len = min(MLEN, len); 1264 m->m_next = n; 1265 } 1266 m = m->m_next; 1267 } 1268out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) 1269 m->m_pkthdr.len = totlen; 1270} 1271 1272/* 1273 * Append the specified data to the indicated mbuf chain, 1274 * Extend the mbuf chain if the new data does not fit in 1275 * existing space. 1276 * 1277 * Return 1 if able to complete the job; otherwise 0. 1278 */ 1279int 1280m_append(struct mbuf *m0, int len, c_caddr_t cp) 1281{ 1282 struct mbuf *m, *n; 1283 int remainder, space; 1284 1285 for (m = m0; m->m_next != NULL; m = m->m_next) 1286 ; 1287 remainder = len; 1288 space = M_TRAILINGSPACE(m); 1289 if (space > 0) { 1290 /* 1291 * Copy into available space. 1292 */ 1293 if (space > remainder) 1294 space = remainder; 1295 bcopy(cp, mtod(m, caddr_t) + m->m_len, space); 1296 m->m_len += space; 1297 cp += space, remainder -= space; 1298 } 1299 while (remainder > 0) { 1300 /* 1301 * Allocate a new mbuf; could check space 1302 * and allocate a cluster instead. 1303 */ 1304 n = m_get(M_DONTWAIT, m->m_type); 1305 if (n == NULL) 1306 break; 1307 n->m_len = min(MLEN, remainder); 1308 bcopy(cp, mtod(n, caddr_t), n->m_len); 1309 cp += n->m_len, remainder -= n->m_len; 1310 m->m_next = n; 1311 m = n; 1312 } 1313 if (m0->m_flags & M_PKTHDR) 1314 m0->m_pkthdr.len += len - remainder; 1315 return (remainder == 0); 1316} 1317 1318/* 1319 * Apply function f to the data in an mbuf chain starting "off" bytes from 1320 * the beginning, continuing for "len" bytes. 1321 */ 1322int 1323m_apply(struct mbuf *m, int off, int len, 1324 int (*f)(void *, void *, u_int), void *arg) 1325{ 1326 u_int count; 1327 int rval; 1328 1329 KASSERT(off >= 0, ("m_apply, negative off %d", off)); 1330 KASSERT(len >= 0, ("m_apply, negative len %d", len)); 1331 while (off > 0) { 1332 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain")); 1333 if (off < m->m_len) 1334 break; 1335 off -= m->m_len; 1336 m = m->m_next; 1337 } 1338 while (len > 0) { 1339 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain")); 1340 count = min(m->m_len - off, len); 1341 rval = (*f)(arg, mtod(m, caddr_t) + off, count); 1342 if (rval) 1343 return (rval); 1344 len -= count; 1345 off = 0; 1346 m = m->m_next; 1347 } 1348 return (0); 1349} 1350 1351/* 1352 * Return a pointer to mbuf/offset of location in mbuf chain. 1353 */ 1354struct mbuf * 1355m_getptr(struct mbuf *m, int loc, int *off) 1356{ 1357 1358 while (loc >= 0) { 1359 /* Normal end of search. */ 1360 if (m->m_len > loc) { 1361 *off = loc; 1362 return (m); 1363 } else { 1364 loc -= m->m_len; 1365 if (m->m_next == NULL) { 1366 if (loc == 0) { 1367 /* Point at the end of valid data. */ 1368 *off = m->m_len; 1369 return (m); 1370 } 1371 return (NULL); 1372 } 1373 m = m->m_next; 1374 } 1375 } 1376 return (NULL); 1377} 1378 1379void 1380m_print(const struct mbuf *m, int maxlen) 1381{ 1382 int len; 1383 int pdata; 1384 const struct mbuf *m2; 1385 1386 if (m->m_flags & M_PKTHDR) 1387 len = m->m_pkthdr.len; 1388 else 1389 len = -1; 1390 m2 = m; 1391 while (m2 != NULL && (len == -1 || len)) { 1392 pdata = m2->m_len; 1393 if (maxlen != -1 && pdata > maxlen) 1394 pdata = maxlen; 1395 printf("mbuf: %p len: %d, next: %p, %b%s", m2, m2->m_len, 1396 m2->m_next, m2->m_flags, "\20\20freelist\17skipfw" 1397 "\11proto5\10proto4\7proto3\6proto2\5proto1\4rdonly" 1398 "\3eor\2pkthdr\1ext", pdata ? "" : "\n"); 1399 if (pdata) 1400 printf(", %*D\n", m2->m_len, (u_char *)m2->m_data, "-"); 1401 if (len != -1) 1402 len -= m2->m_len; 1403 m2 = m2->m_next; 1404 } 1405 if (len > 0) 1406 printf("%d bytes unaccounted for.\n", len); 1407 return; 1408} 1409 1410u_int 1411m_fixhdr(struct mbuf *m0) 1412{ 1413 u_int len; 1414 1415 len = m_length(m0, NULL); 1416 m0->m_pkthdr.len = len; 1417 return (len); 1418} 1419 1420u_int 1421m_length(struct mbuf *m0, struct mbuf **last) 1422{ 1423 struct mbuf *m; 1424 u_int len; 1425 1426 len = 0; 1427 for (m = m0; m != NULL; m = m->m_next) { 1428 len += m->m_len; 1429 if (m->m_next == NULL) 1430 break; 1431 } 1432 if (last != NULL) 1433 *last = m; 1434 return (len); 1435} 1436 1437/* 1438 * Defragment a mbuf chain, returning the shortest possible 1439 * chain of mbufs and clusters. If allocation fails and 1440 * this cannot be completed, NULL will be returned, but 1441 * the passed in chain will be unchanged. Upon success, 1442 * the original chain will be freed, and the new chain 1443 * will be returned. 1444 * 1445 * If a non-packet header is passed in, the original 1446 * mbuf (chain?) will be returned unharmed. 1447 */ 1448struct mbuf * 1449m_defrag(struct mbuf *m0, int how) 1450{ 1451 struct mbuf *m_new = NULL, *m_final = NULL; 1452 int progress = 0, length; 1453 1454 MBUF_CHECKSLEEP(how); 1455 if (!(m0->m_flags & M_PKTHDR)) 1456 return (m0); 1457 1458 m_fixhdr(m0); /* Needed sanity check */ 1459 1460#ifdef MBUF_STRESS_TEST 1461 if (m_defragrandomfailures) { 1462 int temp = arc4random() & 0xff; 1463 if (temp == 0xba) 1464 goto nospace; 1465 } 1466#endif 1467 1468 if (m0->m_pkthdr.len > MHLEN) 1469 m_final = m_getcl(how, MT_DATA, M_PKTHDR); 1470 else 1471 m_final = m_gethdr(how, MT_DATA); 1472 1473 if (m_final == NULL) 1474 goto nospace; 1475 1476 if (m_dup_pkthdr(m_final, m0, how) == 0) 1477 goto nospace; 1478 1479 m_new = m_final; 1480 1481 while (progress < m0->m_pkthdr.len) { 1482 length = m0->m_pkthdr.len - progress; 1483 if (length > MCLBYTES) 1484 length = MCLBYTES; 1485 1486 if (m_new == NULL) { 1487 if (length > MLEN) 1488 m_new = m_getcl(how, MT_DATA, 0); 1489 else 1490 m_new = m_get(how, MT_DATA); 1491 if (m_new == NULL) 1492 goto nospace; 1493 } 1494 1495 m_copydata(m0, progress, length, mtod(m_new, caddr_t)); 1496 progress += length; 1497 m_new->m_len = length; 1498 if (m_new != m_final) 1499 m_cat(m_final, m_new); 1500 m_new = NULL; 1501 } 1502#ifdef MBUF_STRESS_TEST 1503 if (m0->m_next == NULL) 1504 m_defraguseless++; 1505#endif 1506 m_freem(m0); 1507 m0 = m_final; 1508#ifdef MBUF_STRESS_TEST 1509 m_defragpackets++; 1510 m_defragbytes += m0->m_pkthdr.len; 1511#endif 1512 return (m0); 1513nospace: 1514#ifdef MBUF_STRESS_TEST 1515 m_defragfailure++; 1516#endif 1517 if (m_final) 1518 m_freem(m_final); 1519 return (NULL); 1520} 1521 1522#ifdef MBUF_STRESS_TEST 1523 1524/* 1525 * Fragment an mbuf chain. There's no reason you'd ever want to do 1526 * this in normal usage, but it's great for stress testing various 1527 * mbuf consumers. 1528 * 1529 * If fragmentation is not possible, the original chain will be 1530 * returned. 1531 * 1532 * Possible length values: 1533 * 0 no fragmentation will occur 1534 * > 0 each fragment will be of the specified length 1535 * -1 each fragment will be the same random value in length 1536 * -2 each fragment's length will be entirely random 1537 * (Random values range from 1 to 256) 1538 */ 1539struct mbuf * 1540m_fragment(struct mbuf *m0, int how, int length) 1541{ 1542 struct mbuf *m_new = NULL, *m_final = NULL; 1543 int progress = 0; 1544 1545 if (!(m0->m_flags & M_PKTHDR)) 1546 return (m0); 1547 1548 if ((length == 0) || (length < -2)) 1549 return (m0); 1550 1551 m_fixhdr(m0); /* Needed sanity check */ 1552 1553 m_final = m_getcl(how, MT_DATA, M_PKTHDR); 1554 1555 if (m_final == NULL) 1556 goto nospace; 1557 1558 if (m_dup_pkthdr(m_final, m0, how) == 0) 1559 goto nospace; 1560 1561 m_new = m_final; 1562 1563 if (length == -1) 1564 length = 1 + (arc4random() & 255); 1565 1566 while (progress < m0->m_pkthdr.len) { 1567 int fraglen; 1568 1569 if (length > 0) 1570 fraglen = length; 1571 else 1572 fraglen = 1 + (arc4random() & 255); 1573 if (fraglen > m0->m_pkthdr.len - progress) 1574 fraglen = m0->m_pkthdr.len - progress; 1575 1576 if (fraglen > MCLBYTES) 1577 fraglen = MCLBYTES; 1578 1579 if (m_new == NULL) { 1580 m_new = m_getcl(how, MT_DATA, 0); 1581 if (m_new == NULL) 1582 goto nospace; 1583 } 1584 1585 m_copydata(m0, progress, fraglen, mtod(m_new, caddr_t)); 1586 progress += fraglen; 1587 m_new->m_len = fraglen; 1588 if (m_new != m_final) 1589 m_cat(m_final, m_new); 1590 m_new = NULL; 1591 } 1592 m_freem(m0); 1593 m0 = m_final; 1594 return (m0); 1595nospace: 1596 if (m_final) 1597 m_freem(m_final); 1598 /* Return the original chain on failure */ 1599 return (m0); 1600} 1601 1602#endif 1603 1604struct mbuf * 1605m_uiotombuf(struct uio *uio, int how, int len, int align) 1606{ 1607 struct mbuf *m_new = NULL, *m_final = NULL; 1608 int progress = 0, error = 0, length, total; 1609 1610 if (len > 0) 1611 total = min(uio->uio_resid, len); 1612 else 1613 total = uio->uio_resid; 1614 if (align >= MHLEN) 1615 goto nospace; 1616 if (total + align > MHLEN) 1617 m_final = m_getcl(how, MT_DATA, M_PKTHDR); 1618 else 1619 m_final = m_gethdr(how, MT_DATA); 1620 if (m_final == NULL) 1621 goto nospace; 1622 m_final->m_data += align; 1623 m_new = m_final; 1624 while (progress < total) { 1625 length = total - progress; 1626 if (length > MCLBYTES) 1627 length = MCLBYTES; 1628 if (m_new == NULL) { 1629 if (length > MLEN) 1630 m_new = m_getcl(how, MT_DATA, 0); 1631 else 1632 m_new = m_get(how, MT_DATA); 1633 if (m_new == NULL) 1634 goto nospace; 1635 } 1636 error = uiomove(mtod(m_new, void *), length, uio); 1637 if (error) 1638 goto nospace; 1639 progress += length; 1640 m_new->m_len = length; 1641 if (m_new != m_final) 1642 m_cat(m_final, m_new); 1643 m_new = NULL; 1644 } 1645 m_fixhdr(m_final); 1646 return (m_final); 1647nospace: 1648 if (m_new) 1649 m_free(m_new); 1650 if (m_final) 1651 m_freem(m_final); 1652 return (NULL); 1653} 1654 1655/* 1656 * Set the m_data pointer of a newly-allocated mbuf 1657 * to place an object of the specified size at the 1658 * end of the mbuf, longword aligned. 1659 */ 1660void 1661m_align(struct mbuf *m, int len) 1662{ 1663 int adjust; 1664 1665 if (m->m_flags & M_EXT) 1666 adjust = m->m_ext.ext_size - len; 1667 else if (m->m_flags & M_PKTHDR) 1668 adjust = MHLEN - len; 1669 else 1670 adjust = MLEN - len; 1671 m->m_data += adjust &~ (sizeof(long)-1); 1672}
| 223 uma_zfree(zone_pack, m); 224 return; /* Job done. */ 225 break; 226 case EXT_JUMBO9: 227 uma_zfree(zone_jumbo9, m->m_ext.ext_buf); 228 break; 229 case EXT_JUMBO16: 230 uma_zfree(zone_jumbo16, m->m_ext.ext_buf); 231 break; 232 case EXT_SFBUF: 233 case EXT_NET_DRV: 234 case EXT_MOD_TYPE: 235 case EXT_DISPOSABLE: 236 *(m->m_ext.ref_cnt) = 0; 237 uma_zfree(zone_ext_refcnt, __DEVOLATILE(u_int *, 238 m->m_ext.ref_cnt)); 239 /* FALLTHROUGH */ 240 case EXT_EXTREF: 241 KASSERT(m->m_ext.ext_free != NULL, 242 ("%s: ext_free not set", __func__)); 243 (*(m->m_ext.ext_free))(m->m_ext.ext_buf, 244 m->m_ext.ext_args); 245 break; 246 default: 247 KASSERT(m->m_ext.ext_type == 0, 248 ("%s: unknown ext_type", __func__)); 249 } 250 } 251 /* 252 * Free this mbuf back to the mbuf zone with all m_ext 253 * information purged. 254 */ 255 m->m_ext.ext_buf = NULL; 256 m->m_ext.ext_free = NULL; 257 m->m_ext.ext_args = NULL; 258 m->m_ext.ref_cnt = NULL; 259 m->m_ext.ext_size = 0; 260 m->m_ext.ext_type = 0; 261 m->m_flags &= ~M_EXT; 262 uma_zfree(zone_mbuf, m); 263} 264 265/* 266 * Attach the the cluster from *m to *n, set up m_ext in *n 267 * and bump the refcount of the cluster. 268 */ 269static void 270mb_dupcl(struct mbuf *n, struct mbuf *m) 271{ 272 KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__)); 273 KASSERT(m->m_ext.ref_cnt != NULL, ("%s: ref_cnt not set", __func__)); 274 KASSERT((n->m_flags & M_EXT) == 0, ("%s: M_EXT set", __func__)); 275 276 if (*(m->m_ext.ref_cnt) == 1) 277 *(m->m_ext.ref_cnt) += 1; 278 else 279 atomic_add_int(m->m_ext.ref_cnt, 1); 280 n->m_ext.ext_buf = m->m_ext.ext_buf; 281 n->m_ext.ext_free = m->m_ext.ext_free; 282 n->m_ext.ext_args = m->m_ext.ext_args; 283 n->m_ext.ext_size = m->m_ext.ext_size; 284 n->m_ext.ref_cnt = m->m_ext.ref_cnt; 285 n->m_ext.ext_type = m->m_ext.ext_type; 286 n->m_flags |= M_EXT; 287} 288 289/* 290 * Clean up mbuf (chain) from any tags and packet headers. 291 * If "all" is set then the first mbuf in the chain will be 292 * cleaned too. 293 */ 294void 295m_demote(struct mbuf *m0, int all) 296{ 297 struct mbuf *m; 298 299 for (m = all ? m0 : m0->m_next; m != NULL; m = m->m_next) { 300 if (m->m_flags & M_PKTHDR) { 301 m_tag_delete_chain(m, NULL); 302 m->m_flags &= ~M_PKTHDR; 303 bzero(&m->m_pkthdr, sizeof(struct pkthdr)); 304 } 305 if (m->m_type == MT_HEADER) 306 m->m_type = MT_DATA; 307 if (m != m0 && m->m_nextpkt != NULL) 308 m->m_nextpkt = NULL; 309 m->m_flags = m->m_flags & (M_EXT|M_EOR|M_RDONLY|M_FREELIST); 310 } 311} 312 313/* 314 * Sanity checks on mbuf (chain) for use in KASSERT() and general 315 * debugging. 316 * Returns 0 or panics when bad and 1 on all tests passed. 317 * Sanitize, 0 to run M_SANITY_ACTION, 1 to garble things so they 318 * blow up later. 319 */ 320int 321m_sanity(struct mbuf *m0, int sanitize) 322{ 323 struct mbuf *m; 324 caddr_t a, b; 325 int pktlen = 0; 326 327#define M_SANITY_ACTION(s) return (0) 328/* #define M_SANITY_ACTION(s) panic("mbuf %p: " s, m) */ 329 330 for (m = m0; m != NULL; m = m->m_next) { 331 /* 332 * Basic pointer checks. If any of these fails then some 333 * unrelated kernel memory before or after us is trashed. 334 * No way to recover from that. 335 */ 336 a = ((m->m_flags & M_EXT) ? m->m_ext.ext_buf : 337 ((m->m_flags & M_PKTHDR) ? (caddr_t)(&m->m_pktdat) : 338 (caddr_t)(&m->m_dat)) ); 339 b = (caddr_t)(a + (m->m_flags & M_EXT ? m->m_ext.ext_size : 340 ((m->m_flags & M_PKTHDR) ? MHLEN : MLEN))); 341 if ((caddr_t)m->m_data < a) 342 M_SANITY_ACTION("m_data outside mbuf data range left"); 343 if ((caddr_t)m->m_data > b) 344 M_SANITY_ACTION("m_data outside mbuf data range right"); 345 if ((caddr_t)m->m_data + m->m_len > b) 346 M_SANITY_ACTION("m_data + m_len exeeds mbuf space"); 347 if ((m->m_flags & M_PKTHDR) && m->m_pkthdr.header) { 348 if ((caddr_t)m->m_pkthdr.header < a || 349 (caddr_t)m->m_pkthdr.header > b) 350 M_SANITY_ACTION("m_pkthdr.header outside mbuf data range"); 351 } 352 353 /* m->m_nextpkt may only be set on first mbuf in chain. */ 354 if (m != m0 && m->m_nextpkt != NULL) { 355 if (sanitize) { 356 m_freem(m->m_nextpkt); 357 m->m_nextpkt = (struct mbuf *)0xDEADC0DE; 358 } else 359 M_SANITY_ACTION("m->m_nextpkt on in-chain mbuf"); 360 } 361 362 /* correct type correlations. */ 363 if (m->m_type == MT_HEADER && !(m->m_flags & M_PKTHDR)) { 364 if (sanitize) 365 m->m_type = MT_DATA; 366 else 367 M_SANITY_ACTION("MT_HEADER set but not M_PKTHDR"); 368 } 369 370 /* packet length (not mbuf length!) calculation */ 371 if (m0->m_flags & M_PKTHDR) 372 pktlen += m->m_len; 373 374 /* m_tags may only be attached to first mbuf in chain. */ 375 if (m != m0 && m->m_flags & M_PKTHDR && 376 !SLIST_EMPTY(&m->m_pkthdr.tags)) { 377 if (sanitize) { 378 m_tag_delete_chain(m, NULL); 379 /* put in 0xDEADC0DE perhaps? */ 380 } else 381 M_SANITY_ACTION("m_tags on in-chain mbuf"); 382 } 383 384 /* M_PKTHDR may only be set on first mbuf in chain */ 385 if (m != m0 && m->m_flags & M_PKTHDR) { 386 if (sanitize) { 387 bzero(&m->m_pkthdr, sizeof(m->m_pkthdr)); 388 m->m_flags &= ~M_PKTHDR; 389 /* put in 0xDEADCODE and leave hdr flag in */ 390 } else 391 M_SANITY_ACTION("M_PKTHDR on in-chain mbuf"); 392 } 393 } 394 m = m0; 395 if (pktlen && pktlen != m->m_pkthdr.len) { 396 if (sanitize) 397 m->m_pkthdr.len = 0; 398 else 399 M_SANITY_ACTION("m_pkthdr.len != mbuf chain length"); 400 } 401 return 1; 402 403#undef M_SANITY_ACTION 404} 405 406 407/* 408 * "Move" mbuf pkthdr from "from" to "to". 409 * "from" must have M_PKTHDR set, and "to" must be empty. 410 */ 411void 412m_move_pkthdr(struct mbuf *to, struct mbuf *from) 413{ 414 415#if 0 416 /* see below for why these are not enabled */ 417 M_ASSERTPKTHDR(to); 418 /* Note: with MAC, this may not be a good assertion. */ 419 KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), 420 ("m_move_pkthdr: to has tags")); 421#endif 422#ifdef MAC 423 /* 424 * XXXMAC: It could be this should also occur for non-MAC? 425 */ 426 if (to->m_flags & M_PKTHDR) 427 m_tag_delete_chain(to, NULL); 428#endif 429 to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT); 430 if ((to->m_flags & M_EXT) == 0) 431 to->m_data = to->m_pktdat; 432 to->m_pkthdr = from->m_pkthdr; /* especially tags */ 433 SLIST_INIT(&from->m_pkthdr.tags); /* purge tags from src */ 434 from->m_flags &= ~M_PKTHDR; 435} 436 437/* 438 * Duplicate "from"'s mbuf pkthdr in "to". 439 * "from" must have M_PKTHDR set, and "to" must be empty. 440 * In particular, this does a deep copy of the packet tags. 441 */ 442int 443m_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how) 444{ 445 446#if 0 447 /* 448 * The mbuf allocator only initializes the pkthdr 449 * when the mbuf is allocated with MGETHDR. Many users 450 * (e.g. m_copy*, m_prepend) use MGET and then 451 * smash the pkthdr as needed causing these 452 * assertions to trip. For now just disable them. 453 */ 454 M_ASSERTPKTHDR(to); 455 /* Note: with MAC, this may not be a good assertion. */ 456 KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), ("m_dup_pkthdr: to has tags")); 457#endif 458 MBUF_CHECKSLEEP(how); 459#ifdef MAC 460 if (to->m_flags & M_PKTHDR) 461 m_tag_delete_chain(to, NULL); 462#endif 463 to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT); 464 if ((to->m_flags & M_EXT) == 0) 465 to->m_data = to->m_pktdat; 466 to->m_pkthdr = from->m_pkthdr; 467 SLIST_INIT(&to->m_pkthdr.tags); 468 return (m_tag_copy_chain(to, from, MBTOM(how))); 469} 470 471/* 472 * Lesser-used path for M_PREPEND: 473 * allocate new mbuf to prepend to chain, 474 * copy junk along. 475 */ 476struct mbuf * 477m_prepend(struct mbuf *m, int len, int how) 478{ 479 struct mbuf *mn; 480 481 if (m->m_flags & M_PKTHDR) 482 MGETHDR(mn, how, m->m_type); 483 else 484 MGET(mn, how, m->m_type); 485 if (mn == NULL) { 486 m_freem(m); 487 return (NULL); 488 } 489 if (m->m_flags & M_PKTHDR) 490 M_MOVE_PKTHDR(mn, m); 491 mn->m_next = m; 492 m = mn; 493 if (len < MHLEN) 494 MH_ALIGN(m, len); 495 m->m_len = len; 496 return (m); 497} 498 499/* 500 * Make a copy of an mbuf chain starting "off0" bytes from the beginning, 501 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf. 502 * The wait parameter is a choice of M_TRYWAIT/M_DONTWAIT from caller. 503 * Note that the copy is read-only, because clusters are not copied, 504 * only their reference counts are incremented. 505 */ 506struct mbuf * 507m_copym(struct mbuf *m, int off0, int len, int wait) 508{ 509 struct mbuf *n, **np; 510 int off = off0; 511 struct mbuf *top; 512 int copyhdr = 0; 513 514 KASSERT(off >= 0, ("m_copym, negative off %d", off)); 515 KASSERT(len >= 0, ("m_copym, negative len %d", len)); 516 MBUF_CHECKSLEEP(wait); 517 if (off == 0 && m->m_flags & M_PKTHDR) 518 copyhdr = 1; 519 while (off > 0) { 520 KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain")); 521 if (off < m->m_len) 522 break; 523 off -= m->m_len; 524 m = m->m_next; 525 } 526 np = ⊤ 527 top = 0; 528 while (len > 0) { 529 if (m == NULL) { 530 KASSERT(len == M_COPYALL, 531 ("m_copym, length > size of mbuf chain")); 532 break; 533 } 534 if (copyhdr) 535 MGETHDR(n, wait, m->m_type); 536 else 537 MGET(n, wait, m->m_type); 538 *np = n; 539 if (n == NULL) 540 goto nospace; 541 if (copyhdr) { 542 if (!m_dup_pkthdr(n, m, wait)) 543 goto nospace; 544 if (len == M_COPYALL) 545 n->m_pkthdr.len -= off0; 546 else 547 n->m_pkthdr.len = len; 548 copyhdr = 0; 549 } 550 n->m_len = min(len, m->m_len - off); 551 if (m->m_flags & M_EXT) { 552 n->m_data = m->m_data + off; 553 mb_dupcl(n, m); 554 } else 555 bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), 556 (u_int)n->m_len); 557 if (len != M_COPYALL) 558 len -= n->m_len; 559 off = 0; 560 m = m->m_next; 561 np = &n->m_next; 562 } 563 if (top == NULL) 564 mbstat.m_mcfail++; /* XXX: No consistency. */ 565 566 return (top); 567nospace: 568 m_freem(top); 569 mbstat.m_mcfail++; /* XXX: No consistency. */ 570 return (NULL); 571} 572 573/* 574 * Returns mbuf chain with new head for the prepending case. 575 * Copies from mbuf (chain) n from off for len to mbuf (chain) m 576 * either prepending or appending the data. 577 * The resulting mbuf (chain) m is fully writeable. 578 * m is destination (is made writeable) 579 * n is source, off is offset in source, len is len from offset 580 * dir, 0 append, 1 prepend 581 * how, wait or nowait 582 */ 583 584static int 585m_bcopyxxx(void *s, void *t, u_int len) 586{ 587 bcopy(s, t, (size_t)len); 588 return 0; 589} 590 591struct mbuf * 592m_copymdata(struct mbuf *m, struct mbuf *n, int off, int len, 593 int prep, int how) 594{ 595 struct mbuf *mm, *x, *z, *prev = NULL; 596 caddr_t p; 597 int i, nlen = 0; 598 caddr_t buf[MLEN]; 599 600 KASSERT(m != NULL && n != NULL, ("m_copymdata, no target or source")); 601 KASSERT(off >= 0, ("m_copymdata, negative off %d", off)); 602 KASSERT(len >= 0, ("m_copymdata, negative len %d", len)); 603 KASSERT(prep == 0 || prep == 1, ("m_copymdata, unknown direction %d", prep)); 604 605 mm = m; 606 if (!prep) { 607 while(mm->m_next) { 608 prev = mm; 609 mm = mm->m_next; 610 } 611 } 612 for (z = n; z != NULL; z = z->m_next) 613 nlen += z->m_len; 614 if (len == M_COPYALL) 615 len = nlen - off; 616 if (off + len > nlen || len < 1) 617 return NULL; 618 619 if (!M_WRITABLE(mm)) { 620 /* XXX: Use proper m_xxx function instead. */ 621 x = m_getcl(how, MT_DATA, mm->m_flags); 622 if (x == NULL) 623 return NULL; 624 bcopy(mm->m_ext.ext_buf, x->m_ext.ext_buf, x->m_ext.ext_size); 625 p = x->m_ext.ext_buf + (mm->m_data - mm->m_ext.ext_buf); 626 x->m_data = p; 627 mm->m_next = NULL; 628 if (mm != m) 629 prev->m_next = x; 630 m_free(mm); 631 mm = x; 632 } 633 634 /* 635 * Append/prepend the data. Allocating mbufs as necessary. 636 */ 637 /* Shortcut if enough free space in first/last mbuf. */ 638 if (!prep && M_TRAILINGSPACE(mm) >= len) { 639 m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t) + 640 mm->m_len); 641 mm->m_len += len; 642 mm->m_pkthdr.len += len; 643 return m; 644 } 645 if (prep && M_LEADINGSPACE(mm) >= len) { 646 mm->m_data = mtod(mm, caddr_t) - len; 647 m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t)); 648 mm->m_len += len; 649 mm->m_pkthdr.len += len; 650 return mm; 651 } 652 653 /* Expand first/last mbuf to cluster if possible. */ 654 if (!prep && !(mm->m_flags & M_EXT) && len > M_TRAILINGSPACE(mm)) { 655 bcopy(mm->m_data, &buf, mm->m_len); 656 m_clget(mm, how); 657 if (!(mm->m_flags & M_EXT)) 658 return NULL; 659 bcopy(&buf, mm->m_ext.ext_buf, mm->m_len); 660 mm->m_data = mm->m_ext.ext_buf; 661 mm->m_pkthdr.header = NULL; 662 } 663 if (prep && !(mm->m_flags & M_EXT) && len > M_LEADINGSPACE(mm)) { 664 bcopy(mm->m_data, &buf, mm->m_len); 665 m_clget(mm, how); 666 if (!(mm->m_flags & M_EXT)) 667 return NULL; 668 bcopy(&buf, (caddr_t *)mm->m_ext.ext_buf + 669 mm->m_ext.ext_size - mm->m_len, mm->m_len); 670 mm->m_data = (caddr_t)mm->m_ext.ext_buf + 671 mm->m_ext.ext_size - mm->m_len; 672 mm->m_pkthdr.header = NULL; 673 } 674 675 /* Append/prepend as many mbuf (clusters) as necessary to fit len. */ 676 if (!prep && len > M_TRAILINGSPACE(mm)) { 677 if (!m_getm(mm, len - M_TRAILINGSPACE(mm), how, MT_DATA)) 678 return NULL; 679 } 680 if (prep && len > M_LEADINGSPACE(mm)) { 681 if (!(z = m_getm(NULL, len - M_LEADINGSPACE(mm), how, MT_DATA))) 682 return NULL; 683 i = 0; 684 for (x = z; x != NULL; x = x->m_next) { 685 i += x->m_flags & M_EXT ? x->m_ext.ext_size : 686 (x->m_flags & M_PKTHDR ? MHLEN : MLEN); 687 if (!x->m_next) 688 break; 689 } 690 z->m_data += i - len; 691 m_move_pkthdr(mm, z); 692 x->m_next = mm; 693 mm = z; 694 } 695 696 /* Seek to start position in source mbuf. Optimization for long chains. */ 697 while (off > 0) { 698 if (off < n->m_len) 699 break; 700 off -= n->m_len; 701 n = n->m_next; 702 } 703 704 /* Copy data into target mbuf. */ 705 z = mm; 706 while (len > 0) { 707 KASSERT(z != NULL, ("m_copymdata, falling off target edge")); 708 i = M_TRAILINGSPACE(z); 709 m_apply(n, off, i, m_bcopyxxx, mtod(z, caddr_t) + z->m_len); 710 z->m_len += i; 711 /* fixup pkthdr.len if necessary */ 712 if ((prep ? mm : m)->m_flags & M_PKTHDR) 713 (prep ? mm : m)->m_pkthdr.len += i; 714 off += i; 715 len -= i; 716 z = z->m_next; 717 } 718 return (prep ? mm : m); 719} 720 721/* 722 * Copy an entire packet, including header (which must be present). 723 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'. 724 * Note that the copy is read-only, because clusters are not copied, 725 * only their reference counts are incremented. 726 * Preserve alignment of the first mbuf so if the creator has left 727 * some room at the beginning (e.g. for inserting protocol headers) 728 * the copies still have the room available. 729 */ 730struct mbuf * 731m_copypacket(struct mbuf *m, int how) 732{ 733 struct mbuf *top, *n, *o; 734 735 MBUF_CHECKSLEEP(how); 736 MGET(n, how, m->m_type); 737 top = n; 738 if (n == NULL) 739 goto nospace; 740 741 if (!m_dup_pkthdr(n, m, how)) 742 goto nospace; 743 n->m_len = m->m_len; 744 if (m->m_flags & M_EXT) { 745 n->m_data = m->m_data; 746 mb_dupcl(n, m); 747 } else { 748 n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat ); 749 bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 750 } 751 752 m = m->m_next; 753 while (m) { 754 MGET(o, how, m->m_type); 755 if (o == NULL) 756 goto nospace; 757 758 n->m_next = o; 759 n = n->m_next; 760 761 n->m_len = m->m_len; 762 if (m->m_flags & M_EXT) { 763 n->m_data = m->m_data; 764 mb_dupcl(n, m); 765 } else { 766 bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 767 } 768 769 m = m->m_next; 770 } 771 return top; 772nospace: 773 m_freem(top); 774 mbstat.m_mcfail++; /* XXX: No consistency. */ 775 return (NULL); 776} 777 778/* 779 * Copy data from an mbuf chain starting "off" bytes from the beginning, 780 * continuing for "len" bytes, into the indicated buffer. 781 */ 782void 783m_copydata(const struct mbuf *m, int off, int len, caddr_t cp) 784{ 785 u_int count; 786 787 KASSERT(off >= 0, ("m_copydata, negative off %d", off)); 788 KASSERT(len >= 0, ("m_copydata, negative len %d", len)); 789 while (off > 0) { 790 KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain")); 791 if (off < m->m_len) 792 break; 793 off -= m->m_len; 794 m = m->m_next; 795 } 796 while (len > 0) { 797 KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain")); 798 count = min(m->m_len - off, len); 799 bcopy(mtod(m, caddr_t) + off, cp, count); 800 len -= count; 801 cp += count; 802 off = 0; 803 m = m->m_next; 804 } 805} 806 807/* 808 * Copy a packet header mbuf chain into a completely new chain, including 809 * copying any mbuf clusters. Use this instead of m_copypacket() when 810 * you need a writable copy of an mbuf chain. 811 */ 812struct mbuf * 813m_dup(struct mbuf *m, int how) 814{ 815 struct mbuf **p, *top = NULL; 816 int remain, moff, nsize; 817 818 MBUF_CHECKSLEEP(how); 819 /* Sanity check */ 820 if (m == NULL) 821 return (NULL); 822 M_ASSERTPKTHDR(m); 823 824 /* While there's more data, get a new mbuf, tack it on, and fill it */ 825 remain = m->m_pkthdr.len; 826 moff = 0; 827 p = ⊤ 828 while (remain > 0 || top == NULL) { /* allow m->m_pkthdr.len == 0 */ 829 struct mbuf *n; 830 831 /* Get the next new mbuf */ 832 if (remain >= MINCLSIZE) { 833 n = m_getcl(how, m->m_type, 0); 834 nsize = MCLBYTES; 835 } else { 836 n = m_get(how, m->m_type); 837 nsize = MLEN; 838 } 839 if (n == NULL) 840 goto nospace; 841 842 if (top == NULL) { /* First one, must be PKTHDR */ 843 if (!m_dup_pkthdr(n, m, how)) { 844 m_free(n); 845 goto nospace; 846 } 847 nsize = MHLEN; 848 } 849 n->m_len = 0; 850 851 /* Link it into the new chain */ 852 *p = n; 853 p = &n->m_next; 854 855 /* Copy data from original mbuf(s) into new mbuf */ 856 while (n->m_len < nsize && m != NULL) { 857 int chunk = min(nsize - n->m_len, m->m_len - moff); 858 859 bcopy(m->m_data + moff, n->m_data + n->m_len, chunk); 860 moff += chunk; 861 n->m_len += chunk; 862 remain -= chunk; 863 if (moff == m->m_len) { 864 m = m->m_next; 865 moff = 0; 866 } 867 } 868 869 /* Check correct total mbuf length */ 870 KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL), 871 ("%s: bogus m_pkthdr.len", __func__)); 872 } 873 return (top); 874 875nospace: 876 m_freem(top); 877 mbstat.m_mcfail++; /* XXX: No consistency. */ 878 return (NULL); 879} 880 881/* 882 * Concatenate mbuf chain n to m. 883 * Both chains must be of the same type (e.g. MT_DATA). 884 * Any m_pkthdr is not updated. 885 */ 886void 887m_cat(struct mbuf *m, struct mbuf *n) 888{ 889 while (m->m_next) 890 m = m->m_next; 891 while (n) { 892 if (m->m_flags & M_EXT || 893 m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) { 894 /* just join the two chains */ 895 m->m_next = n; 896 return; 897 } 898 /* splat the data from one into the other */ 899 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 900 (u_int)n->m_len); 901 m->m_len += n->m_len; 902 n = m_free(n); 903 } 904} 905 906void 907m_adj(struct mbuf *mp, int req_len) 908{ 909 int len = req_len; 910 struct mbuf *m; 911 int count; 912 913 if ((m = mp) == NULL) 914 return; 915 if (len >= 0) { 916 /* 917 * Trim from head. 918 */ 919 while (m != NULL && len > 0) { 920 if (m->m_len <= len) { 921 len -= m->m_len; 922 m->m_len = 0; 923 m = m->m_next; 924 } else { 925 m->m_len -= len; 926 m->m_data += len; 927 len = 0; 928 } 929 } 930 m = mp; 931 if (mp->m_flags & M_PKTHDR) 932 m->m_pkthdr.len -= (req_len - len); 933 } else { 934 /* 935 * Trim from tail. Scan the mbuf chain, 936 * calculating its length and finding the last mbuf. 937 * If the adjustment only affects this mbuf, then just 938 * adjust and return. Otherwise, rescan and truncate 939 * after the remaining size. 940 */ 941 len = -len; 942 count = 0; 943 for (;;) { 944 count += m->m_len; 945 if (m->m_next == (struct mbuf *)0) 946 break; 947 m = m->m_next; 948 } 949 if (m->m_len >= len) { 950 m->m_len -= len; 951 if (mp->m_flags & M_PKTHDR) 952 mp->m_pkthdr.len -= len; 953 return; 954 } 955 count -= len; 956 if (count < 0) 957 count = 0; 958 /* 959 * Correct length for chain is "count". 960 * Find the mbuf with last data, adjust its length, 961 * and toss data from remaining mbufs on chain. 962 */ 963 m = mp; 964 if (m->m_flags & M_PKTHDR) 965 m->m_pkthdr.len = count; 966 for (; m; m = m->m_next) { 967 if (m->m_len >= count) { 968 m->m_len = count; 969 if (m->m_next != NULL) { 970 m_freem(m->m_next); 971 m->m_next = NULL; 972 } 973 break; 974 } 975 count -= m->m_len; 976 } 977 } 978} 979 980/* 981 * Rearange an mbuf chain so that len bytes are contiguous 982 * and in the data area of an mbuf (so that mtod and dtom 983 * will work for a structure of size len). Returns the resulting 984 * mbuf chain on success, frees it and returns null on failure. 985 * If there is room, it will add up to max_protohdr-len extra bytes to the 986 * contiguous region in an attempt to avoid being called next time. 987 */ 988struct mbuf * 989m_pullup(struct mbuf *n, int len) 990{ 991 struct mbuf *m; 992 int count; 993 int space; 994 995 /* 996 * If first mbuf has no cluster, and has room for len bytes 997 * without shifting current data, pullup into it, 998 * otherwise allocate a new mbuf to prepend to the chain. 999 */ 1000 if ((n->m_flags & M_EXT) == 0 && 1001 n->m_data + len < &n->m_dat[MLEN] && n->m_next) { 1002 if (n->m_len >= len) 1003 return (n); 1004 m = n; 1005 n = n->m_next; 1006 len -= m->m_len; 1007 } else { 1008 if (len > MHLEN) 1009 goto bad; 1010 MGET(m, M_DONTWAIT, n->m_type); 1011 if (m == NULL) 1012 goto bad; 1013 m->m_len = 0; 1014 if (n->m_flags & M_PKTHDR) 1015 M_MOVE_PKTHDR(m, n); 1016 } 1017 space = &m->m_dat[MLEN] - (m->m_data + m->m_len); 1018 do { 1019 count = min(min(max(len, max_protohdr), space), n->m_len); 1020 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 1021 (u_int)count); 1022 len -= count; 1023 m->m_len += count; 1024 n->m_len -= count; 1025 space -= count; 1026 if (n->m_len) 1027 n->m_data += count; 1028 else 1029 n = m_free(n); 1030 } while (len > 0 && n); 1031 if (len > 0) { 1032 (void) m_free(m); 1033 goto bad; 1034 } 1035 m->m_next = n; 1036 return (m); 1037bad: 1038 m_freem(n); 1039 mbstat.m_mpfail++; /* XXX: No consistency. */ 1040 return (NULL); 1041} 1042 1043/* 1044 * Like m_pullup(), except a new mbuf is always allocated, and we allow 1045 * the amount of empty space before the data in the new mbuf to be specified 1046 * (in the event that the caller expects to prepend later). 1047 */ 1048int MSFail; 1049 1050struct mbuf * 1051m_copyup(struct mbuf *n, int len, int dstoff) 1052{ 1053 struct mbuf *m; 1054 int count, space; 1055 1056 if (len > (MHLEN - dstoff)) 1057 goto bad; 1058 MGET(m, M_DONTWAIT, n->m_type); 1059 if (m == NULL) 1060 goto bad; 1061 m->m_len = 0; 1062 if (n->m_flags & M_PKTHDR) 1063 M_MOVE_PKTHDR(m, n); 1064 m->m_data += dstoff; 1065 space = &m->m_dat[MLEN] - (m->m_data + m->m_len); 1066 do { 1067 count = min(min(max(len, max_protohdr), space), n->m_len); 1068 memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t), 1069 (unsigned)count); 1070 len -= count; 1071 m->m_len += count; 1072 n->m_len -= count; 1073 space -= count; 1074 if (n->m_len) 1075 n->m_data += count; 1076 else 1077 n = m_free(n); 1078 } while (len > 0 && n); 1079 if (len > 0) { 1080 (void) m_free(m); 1081 goto bad; 1082 } 1083 m->m_next = n; 1084 return (m); 1085 bad: 1086 m_freem(n); 1087 MSFail++; 1088 return (NULL); 1089} 1090 1091/* 1092 * Partition an mbuf chain in two pieces, returning the tail -- 1093 * all but the first len0 bytes. In case of failure, it returns NULL and 1094 * attempts to restore the chain to its original state. 1095 * 1096 * Note that the resulting mbufs might be read-only, because the new 1097 * mbuf can end up sharing an mbuf cluster with the original mbuf if 1098 * the "breaking point" happens to lie within a cluster mbuf. Use the 1099 * M_WRITABLE() macro to check for this case. 1100 */ 1101struct mbuf * 1102m_split(struct mbuf *m0, int len0, int wait) 1103{ 1104 struct mbuf *m, *n; 1105 u_int len = len0, remain; 1106 1107 MBUF_CHECKSLEEP(wait); 1108 for (m = m0; m && len > m->m_len; m = m->m_next) 1109 len -= m->m_len; 1110 if (m == NULL) 1111 return (NULL); 1112 remain = m->m_len - len; 1113 if (m0->m_flags & M_PKTHDR) { 1114 MGETHDR(n, wait, m0->m_type); 1115 if (n == NULL) 1116 return (NULL); 1117 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif; 1118 n->m_pkthdr.len = m0->m_pkthdr.len - len0; 1119 m0->m_pkthdr.len = len0; 1120 if (m->m_flags & M_EXT) 1121 goto extpacket; 1122 if (remain > MHLEN) { 1123 /* m can't be the lead packet */ 1124 MH_ALIGN(n, 0); 1125 n->m_next = m_split(m, len, wait); 1126 if (n->m_next == NULL) { 1127 (void) m_free(n); 1128 return (NULL); 1129 } else { 1130 n->m_len = 0; 1131 return (n); 1132 } 1133 } else 1134 MH_ALIGN(n, remain); 1135 } else if (remain == 0) { 1136 n = m->m_next; 1137 m->m_next = NULL; 1138 return (n); 1139 } else { 1140 MGET(n, wait, m->m_type); 1141 if (n == NULL) 1142 return (NULL); 1143 M_ALIGN(n, remain); 1144 } 1145extpacket: 1146 if (m->m_flags & M_EXT) { 1147 n->m_data = m->m_data + len; 1148 mb_dupcl(n, m); 1149 } else { 1150 bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain); 1151 } 1152 n->m_len = remain; 1153 m->m_len = len; 1154 n->m_next = m->m_next; 1155 m->m_next = NULL; 1156 return (n); 1157} 1158/* 1159 * Routine to copy from device local memory into mbufs. 1160 * Note that `off' argument is offset into first mbuf of target chain from 1161 * which to begin copying the data to. 1162 */ 1163struct mbuf * 1164m_devget(char *buf, int totlen, int off, struct ifnet *ifp, 1165 void (*copy)(char *from, caddr_t to, u_int len)) 1166{ 1167 struct mbuf *m; 1168 struct mbuf *top = NULL, **mp = ⊤ 1169 int len; 1170 1171 if (off < 0 || off > MHLEN) 1172 return (NULL); 1173 1174 while (totlen > 0) { 1175 if (top == NULL) { /* First one, must be PKTHDR */ 1176 if (totlen + off >= MINCLSIZE) { 1177 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1178 len = MCLBYTES; 1179 } else { 1180 m = m_gethdr(M_DONTWAIT, MT_DATA); 1181 len = MHLEN; 1182 1183 /* Place initial small packet/header at end of mbuf */ 1184 if (m && totlen + off + max_linkhdr <= MLEN) { 1185 m->m_data += max_linkhdr; 1186 len -= max_linkhdr; 1187 } 1188 } 1189 if (m == NULL) 1190 return NULL; 1191 m->m_pkthdr.rcvif = ifp; 1192 m->m_pkthdr.len = totlen; 1193 } else { 1194 if (totlen + off >= MINCLSIZE) { 1195 m = m_getcl(M_DONTWAIT, MT_DATA, 0); 1196 len = MCLBYTES; 1197 } else { 1198 m = m_get(M_DONTWAIT, MT_DATA); 1199 len = MLEN; 1200 } 1201 if (m == NULL) { 1202 m_freem(top); 1203 return NULL; 1204 } 1205 } 1206 if (off) { 1207 m->m_data += off; 1208 len -= off; 1209 off = 0; 1210 } 1211 m->m_len = len = min(totlen, len); 1212 if (copy) 1213 copy(buf, mtod(m, caddr_t), (u_int)len); 1214 else 1215 bcopy(buf, mtod(m, caddr_t), (u_int)len); 1216 buf += len; 1217 *mp = m; 1218 mp = &m->m_next; 1219 totlen -= len; 1220 } 1221 return (top); 1222} 1223 1224/* 1225 * Copy data from a buffer back into the indicated mbuf chain, 1226 * starting "off" bytes from the beginning, extending the mbuf 1227 * chain if necessary. 1228 */ 1229void 1230m_copyback(struct mbuf *m0, int off, int len, c_caddr_t cp) 1231{ 1232 int mlen; 1233 struct mbuf *m = m0, *n; 1234 int totlen = 0; 1235 1236 if (m0 == NULL) 1237 return; 1238 while (off > (mlen = m->m_len)) { 1239 off -= mlen; 1240 totlen += mlen; 1241 if (m->m_next == NULL) { 1242 n = m_get(M_DONTWAIT, m->m_type); 1243 if (n == NULL) 1244 goto out; 1245 bzero(mtod(n, caddr_t), MLEN); 1246 n->m_len = min(MLEN, len + off); 1247 m->m_next = n; 1248 } 1249 m = m->m_next; 1250 } 1251 while (len > 0) { 1252 mlen = min (m->m_len - off, len); 1253 bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen); 1254 cp += mlen; 1255 len -= mlen; 1256 mlen += off; 1257 off = 0; 1258 totlen += mlen; 1259 if (len == 0) 1260 break; 1261 if (m->m_next == NULL) { 1262 n = m_get(M_DONTWAIT, m->m_type); 1263 if (n == NULL) 1264 break; 1265 n->m_len = min(MLEN, len); 1266 m->m_next = n; 1267 } 1268 m = m->m_next; 1269 } 1270out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) 1271 m->m_pkthdr.len = totlen; 1272} 1273 1274/* 1275 * Append the specified data to the indicated mbuf chain, 1276 * Extend the mbuf chain if the new data does not fit in 1277 * existing space. 1278 * 1279 * Return 1 if able to complete the job; otherwise 0. 1280 */ 1281int 1282m_append(struct mbuf *m0, int len, c_caddr_t cp) 1283{ 1284 struct mbuf *m, *n; 1285 int remainder, space; 1286 1287 for (m = m0; m->m_next != NULL; m = m->m_next) 1288 ; 1289 remainder = len; 1290 space = M_TRAILINGSPACE(m); 1291 if (space > 0) { 1292 /* 1293 * Copy into available space. 1294 */ 1295 if (space > remainder) 1296 space = remainder; 1297 bcopy(cp, mtod(m, caddr_t) + m->m_len, space); 1298 m->m_len += space; 1299 cp += space, remainder -= space; 1300 } 1301 while (remainder > 0) { 1302 /* 1303 * Allocate a new mbuf; could check space 1304 * and allocate a cluster instead. 1305 */ 1306 n = m_get(M_DONTWAIT, m->m_type); 1307 if (n == NULL) 1308 break; 1309 n->m_len = min(MLEN, remainder); 1310 bcopy(cp, mtod(n, caddr_t), n->m_len); 1311 cp += n->m_len, remainder -= n->m_len; 1312 m->m_next = n; 1313 m = n; 1314 } 1315 if (m0->m_flags & M_PKTHDR) 1316 m0->m_pkthdr.len += len - remainder; 1317 return (remainder == 0); 1318} 1319 1320/* 1321 * Apply function f to the data in an mbuf chain starting "off" bytes from 1322 * the beginning, continuing for "len" bytes. 1323 */ 1324int 1325m_apply(struct mbuf *m, int off, int len, 1326 int (*f)(void *, void *, u_int), void *arg) 1327{ 1328 u_int count; 1329 int rval; 1330 1331 KASSERT(off >= 0, ("m_apply, negative off %d", off)); 1332 KASSERT(len >= 0, ("m_apply, negative len %d", len)); 1333 while (off > 0) { 1334 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain")); 1335 if (off < m->m_len) 1336 break; 1337 off -= m->m_len; 1338 m = m->m_next; 1339 } 1340 while (len > 0) { 1341 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain")); 1342 count = min(m->m_len - off, len); 1343 rval = (*f)(arg, mtod(m, caddr_t) + off, count); 1344 if (rval) 1345 return (rval); 1346 len -= count; 1347 off = 0; 1348 m = m->m_next; 1349 } 1350 return (0); 1351} 1352 1353/* 1354 * Return a pointer to mbuf/offset of location in mbuf chain. 1355 */ 1356struct mbuf * 1357m_getptr(struct mbuf *m, int loc, int *off) 1358{ 1359 1360 while (loc >= 0) { 1361 /* Normal end of search. */ 1362 if (m->m_len > loc) { 1363 *off = loc; 1364 return (m); 1365 } else { 1366 loc -= m->m_len; 1367 if (m->m_next == NULL) { 1368 if (loc == 0) { 1369 /* Point at the end of valid data. */ 1370 *off = m->m_len; 1371 return (m); 1372 } 1373 return (NULL); 1374 } 1375 m = m->m_next; 1376 } 1377 } 1378 return (NULL); 1379} 1380 1381void 1382m_print(const struct mbuf *m, int maxlen) 1383{ 1384 int len; 1385 int pdata; 1386 const struct mbuf *m2; 1387 1388 if (m->m_flags & M_PKTHDR) 1389 len = m->m_pkthdr.len; 1390 else 1391 len = -1; 1392 m2 = m; 1393 while (m2 != NULL && (len == -1 || len)) { 1394 pdata = m2->m_len; 1395 if (maxlen != -1 && pdata > maxlen) 1396 pdata = maxlen; 1397 printf("mbuf: %p len: %d, next: %p, %b%s", m2, m2->m_len, 1398 m2->m_next, m2->m_flags, "\20\20freelist\17skipfw" 1399 "\11proto5\10proto4\7proto3\6proto2\5proto1\4rdonly" 1400 "\3eor\2pkthdr\1ext", pdata ? "" : "\n"); 1401 if (pdata) 1402 printf(", %*D\n", m2->m_len, (u_char *)m2->m_data, "-"); 1403 if (len != -1) 1404 len -= m2->m_len; 1405 m2 = m2->m_next; 1406 } 1407 if (len > 0) 1408 printf("%d bytes unaccounted for.\n", len); 1409 return; 1410} 1411 1412u_int 1413m_fixhdr(struct mbuf *m0) 1414{ 1415 u_int len; 1416 1417 len = m_length(m0, NULL); 1418 m0->m_pkthdr.len = len; 1419 return (len); 1420} 1421 1422u_int 1423m_length(struct mbuf *m0, struct mbuf **last) 1424{ 1425 struct mbuf *m; 1426 u_int len; 1427 1428 len = 0; 1429 for (m = m0; m != NULL; m = m->m_next) { 1430 len += m->m_len; 1431 if (m->m_next == NULL) 1432 break; 1433 } 1434 if (last != NULL) 1435 *last = m; 1436 return (len); 1437} 1438 1439/* 1440 * Defragment a mbuf chain, returning the shortest possible 1441 * chain of mbufs and clusters. If allocation fails and 1442 * this cannot be completed, NULL will be returned, but 1443 * the passed in chain will be unchanged. Upon success, 1444 * the original chain will be freed, and the new chain 1445 * will be returned. 1446 * 1447 * If a non-packet header is passed in, the original 1448 * mbuf (chain?) will be returned unharmed. 1449 */ 1450struct mbuf * 1451m_defrag(struct mbuf *m0, int how) 1452{ 1453 struct mbuf *m_new = NULL, *m_final = NULL; 1454 int progress = 0, length; 1455 1456 MBUF_CHECKSLEEP(how); 1457 if (!(m0->m_flags & M_PKTHDR)) 1458 return (m0); 1459 1460 m_fixhdr(m0); /* Needed sanity check */ 1461 1462#ifdef MBUF_STRESS_TEST 1463 if (m_defragrandomfailures) { 1464 int temp = arc4random() & 0xff; 1465 if (temp == 0xba) 1466 goto nospace; 1467 } 1468#endif 1469 1470 if (m0->m_pkthdr.len > MHLEN) 1471 m_final = m_getcl(how, MT_DATA, M_PKTHDR); 1472 else 1473 m_final = m_gethdr(how, MT_DATA); 1474 1475 if (m_final == NULL) 1476 goto nospace; 1477 1478 if (m_dup_pkthdr(m_final, m0, how) == 0) 1479 goto nospace; 1480 1481 m_new = m_final; 1482 1483 while (progress < m0->m_pkthdr.len) { 1484 length = m0->m_pkthdr.len - progress; 1485 if (length > MCLBYTES) 1486 length = MCLBYTES; 1487 1488 if (m_new == NULL) { 1489 if (length > MLEN) 1490 m_new = m_getcl(how, MT_DATA, 0); 1491 else 1492 m_new = m_get(how, MT_DATA); 1493 if (m_new == NULL) 1494 goto nospace; 1495 } 1496 1497 m_copydata(m0, progress, length, mtod(m_new, caddr_t)); 1498 progress += length; 1499 m_new->m_len = length; 1500 if (m_new != m_final) 1501 m_cat(m_final, m_new); 1502 m_new = NULL; 1503 } 1504#ifdef MBUF_STRESS_TEST 1505 if (m0->m_next == NULL) 1506 m_defraguseless++; 1507#endif 1508 m_freem(m0); 1509 m0 = m_final; 1510#ifdef MBUF_STRESS_TEST 1511 m_defragpackets++; 1512 m_defragbytes += m0->m_pkthdr.len; 1513#endif 1514 return (m0); 1515nospace: 1516#ifdef MBUF_STRESS_TEST 1517 m_defragfailure++; 1518#endif 1519 if (m_final) 1520 m_freem(m_final); 1521 return (NULL); 1522} 1523 1524#ifdef MBUF_STRESS_TEST 1525 1526/* 1527 * Fragment an mbuf chain. There's no reason you'd ever want to do 1528 * this in normal usage, but it's great for stress testing various 1529 * mbuf consumers. 1530 * 1531 * If fragmentation is not possible, the original chain will be 1532 * returned. 1533 * 1534 * Possible length values: 1535 * 0 no fragmentation will occur 1536 * > 0 each fragment will be of the specified length 1537 * -1 each fragment will be the same random value in length 1538 * -2 each fragment's length will be entirely random 1539 * (Random values range from 1 to 256) 1540 */ 1541struct mbuf * 1542m_fragment(struct mbuf *m0, int how, int length) 1543{ 1544 struct mbuf *m_new = NULL, *m_final = NULL; 1545 int progress = 0; 1546 1547 if (!(m0->m_flags & M_PKTHDR)) 1548 return (m0); 1549 1550 if ((length == 0) || (length < -2)) 1551 return (m0); 1552 1553 m_fixhdr(m0); /* Needed sanity check */ 1554 1555 m_final = m_getcl(how, MT_DATA, M_PKTHDR); 1556 1557 if (m_final == NULL) 1558 goto nospace; 1559 1560 if (m_dup_pkthdr(m_final, m0, how) == 0) 1561 goto nospace; 1562 1563 m_new = m_final; 1564 1565 if (length == -1) 1566 length = 1 + (arc4random() & 255); 1567 1568 while (progress < m0->m_pkthdr.len) { 1569 int fraglen; 1570 1571 if (length > 0) 1572 fraglen = length; 1573 else 1574 fraglen = 1 + (arc4random() & 255); 1575 if (fraglen > m0->m_pkthdr.len - progress) 1576 fraglen = m0->m_pkthdr.len - progress; 1577 1578 if (fraglen > MCLBYTES) 1579 fraglen = MCLBYTES; 1580 1581 if (m_new == NULL) { 1582 m_new = m_getcl(how, MT_DATA, 0); 1583 if (m_new == NULL) 1584 goto nospace; 1585 } 1586 1587 m_copydata(m0, progress, fraglen, mtod(m_new, caddr_t)); 1588 progress += fraglen; 1589 m_new->m_len = fraglen; 1590 if (m_new != m_final) 1591 m_cat(m_final, m_new); 1592 m_new = NULL; 1593 } 1594 m_freem(m0); 1595 m0 = m_final; 1596 return (m0); 1597nospace: 1598 if (m_final) 1599 m_freem(m_final); 1600 /* Return the original chain on failure */ 1601 return (m0); 1602} 1603 1604#endif 1605 1606struct mbuf * 1607m_uiotombuf(struct uio *uio, int how, int len, int align) 1608{ 1609 struct mbuf *m_new = NULL, *m_final = NULL; 1610 int progress = 0, error = 0, length, total; 1611 1612 if (len > 0) 1613 total = min(uio->uio_resid, len); 1614 else 1615 total = uio->uio_resid; 1616 if (align >= MHLEN) 1617 goto nospace; 1618 if (total + align > MHLEN) 1619 m_final = m_getcl(how, MT_DATA, M_PKTHDR); 1620 else 1621 m_final = m_gethdr(how, MT_DATA); 1622 if (m_final == NULL) 1623 goto nospace; 1624 m_final->m_data += align; 1625 m_new = m_final; 1626 while (progress < total) { 1627 length = total - progress; 1628 if (length > MCLBYTES) 1629 length = MCLBYTES; 1630 if (m_new == NULL) { 1631 if (length > MLEN) 1632 m_new = m_getcl(how, MT_DATA, 0); 1633 else 1634 m_new = m_get(how, MT_DATA); 1635 if (m_new == NULL) 1636 goto nospace; 1637 } 1638 error = uiomove(mtod(m_new, void *), length, uio); 1639 if (error) 1640 goto nospace; 1641 progress += length; 1642 m_new->m_len = length; 1643 if (m_new != m_final) 1644 m_cat(m_final, m_new); 1645 m_new = NULL; 1646 } 1647 m_fixhdr(m_final); 1648 return (m_final); 1649nospace: 1650 if (m_new) 1651 m_free(m_new); 1652 if (m_final) 1653 m_freem(m_final); 1654 return (NULL); 1655} 1656 1657/* 1658 * Set the m_data pointer of a newly-allocated mbuf 1659 * to place an object of the specified size at the 1660 * end of the mbuf, longword aligned. 1661 */ 1662void 1663m_align(struct mbuf *m, int len) 1664{ 1665 int adjust; 1666 1667 if (m->m_flags & M_EXT) 1668 adjust = m->m_ext.ext_size - len; 1669 else if (m->m_flags & M_PKTHDR) 1670 adjust = MHLEN - len; 1671 else 1672 adjust = MLEN - len; 1673 m->m_data += adjust &~ (sizeof(long)-1); 1674}
|