1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 29df8bae1dSRodney W. Grimes * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94 30df8bae1dSRodney W. Grimes */ 31df8bae1dSRodney W. Grimes 32677b542eSDavid E. O'Brien #include <sys/cdefs.h> 33677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 34677b542eSDavid E. O'Brien 35e32a5b94SRobert Watson #include "opt_mac.h" 36240ef842SDavid E. O'Brien #include "opt_param.h" 37352d050eSMike Silbersack #include "opt_mbuf_stress_test.h" 38e32a5b94SRobert Watson 39df8bae1dSRodney W. Grimes #include <sys/param.h> 40df8bae1dSRodney W. Grimes #include <sys/systm.h> 41fb919e4dSMark Murray #include <sys/kernel.h> 42beb699c7SMike Silbersack #include <sys/limits.h> 43fb919e4dSMark Murray #include <sys/lock.h> 44e32a5b94SRobert Watson #include <sys/mac.h> 45f9d0d524SRobert Watson #include <sys/malloc.h> 46df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 47639acc13SGarrett Wollman #include <sys/sysctl.h> 48df8bae1dSRodney W. Grimes #include <sys/domain.h> 49df8bae1dSRodney W. Grimes #include <sys/protosw.h> 50beb699c7SMike Silbersack #include <sys/uio.h> 51fb919e4dSMark Murray 5228f8db14SBruce Evans int max_linkhdr; 5328f8db14SBruce Evans int max_protohdr; 5428f8db14SBruce Evans int max_hdr; 5528f8db14SBruce Evans int max_datalen; 5651710a45SMike Silbersack #ifdef MBUF_STRESS_TEST 5755e9f80dSMike Silbersack int m_defragpackets; 5855e9f80dSMike Silbersack int m_defragbytes; 5955e9f80dSMike Silbersack int m_defraguseless; 6055e9f80dSMike Silbersack int m_defragfailure; 61352d050eSMike Silbersack int m_defragrandomfailures; 62352d050eSMike Silbersack #endif 637d032714SBosko Milekic 647d032714SBosko Milekic /* 657d032714SBosko Milekic * sysctl(8) exported objects 667d032714SBosko Milekic */ 67ce02431fSDoug Rabson SYSCTL_DECL(_kern_ipc); 68639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW, 69639acc13SGarrett Wollman &max_linkhdr, 0, ""); 70639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW, 71639acc13SGarrett Wollman &max_protohdr, 0, ""); 72639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, ""); 73639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW, 74639acc13SGarrett Wollman &max_datalen, 0, ""); 7551710a45SMike Silbersack #ifdef MBUF_STRESS_TEST 7655e9f80dSMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragpackets, CTLFLAG_RD, 7755e9f80dSMike Silbersack &m_defragpackets, 0, ""); 7855e9f80dSMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragbytes, CTLFLAG_RD, 7955e9f80dSMike Silbersack &m_defragbytes, 0, ""); 8055e9f80dSMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, m_defraguseless, CTLFLAG_RD, 8155e9f80dSMike Silbersack &m_defraguseless, 0, ""); 8255e9f80dSMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragfailure, CTLFLAG_RD, 8355e9f80dSMike Silbersack &m_defragfailure, 0, ""); 84352d050eSMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragrandomfailures, CTLFLAG_RW, 85352d050eSMike Silbersack &m_defragrandomfailures, 0, ""); 86352d050eSMike Silbersack #endif 87df8bae1dSRodney W. Grimes 88df8bae1dSRodney W. Grimes /* 89099a0e58SBosko Milekic * Malloc-type for external ext_buf ref counts. 90099a0e58SBosko Milekic */ 91099a0e58SBosko Milekic MALLOC_DEFINE(M_MBUF, "mbextcnt", "mbuf external ref counts"); 92099a0e58SBosko Milekic 93099a0e58SBosko Milekic /* 94099a0e58SBosko Milekic * Allocate a given length worth of mbufs and/or clusters (whatever fits 95099a0e58SBosko Milekic * best) and return a pointer to the top of the allocated chain. If an 96099a0e58SBosko Milekic * existing mbuf chain is provided, then we will append the new chain 97099a0e58SBosko Milekic * to the existing one but still return the top of the newly allocated 98099a0e58SBosko Milekic * chain. 99099a0e58SBosko Milekic */ 100099a0e58SBosko Milekic struct mbuf * 101099a0e58SBosko Milekic m_getm(struct mbuf *m, int len, int how, short type) 102099a0e58SBosko Milekic { 103099a0e58SBosko Milekic struct mbuf *mb, *top, *cur, *mtail; 104099a0e58SBosko Milekic int num, rem; 105099a0e58SBosko Milekic int i; 106099a0e58SBosko Milekic 107099a0e58SBosko Milekic KASSERT(len >= 0, ("m_getm(): len is < 0")); 108063d8114SAlfred Perlstein MBUF_CHECKSLEEP(how); 109099a0e58SBosko Milekic 110099a0e58SBosko Milekic /* If m != NULL, we will append to the end of that chain. */ 111099a0e58SBosko Milekic if (m != NULL) 112099a0e58SBosko Milekic for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next); 113099a0e58SBosko Milekic else 114099a0e58SBosko Milekic mtail = NULL; 115099a0e58SBosko Milekic 116099a0e58SBosko Milekic /* 117099a0e58SBosko Milekic * Calculate how many mbufs+clusters ("packets") we need and how much 118099a0e58SBosko Milekic * leftover there is after that and allocate the first mbuf+cluster 119099a0e58SBosko Milekic * if required. 120099a0e58SBosko Milekic */ 121099a0e58SBosko Milekic num = len / MCLBYTES; 122099a0e58SBosko Milekic rem = len % MCLBYTES; 123099a0e58SBosko Milekic top = cur = NULL; 124099a0e58SBosko Milekic if (num > 0) { 125099a0e58SBosko Milekic if ((top = cur = m_getcl(how, type, 0)) == NULL) 126099a0e58SBosko Milekic goto failed; 127931f76abSMaxime Henrion top->m_len = 0; 128099a0e58SBosko Milekic } 129099a0e58SBosko Milekic num--; 130099a0e58SBosko Milekic 131099a0e58SBosko Milekic for (i = 0; i < num; i++) { 132099a0e58SBosko Milekic mb = m_getcl(how, type, 0); 133099a0e58SBosko Milekic if (mb == NULL) 134099a0e58SBosko Milekic goto failed; 135099a0e58SBosko Milekic mb->m_len = 0; 136099a0e58SBosko Milekic cur = (cur->m_next = mb); 137099a0e58SBosko Milekic } 138099a0e58SBosko Milekic if (rem > 0) { 139099a0e58SBosko Milekic mb = (rem > MINCLSIZE) ? 140099a0e58SBosko Milekic m_getcl(how, type, 0) : m_get(how, type); 141099a0e58SBosko Milekic if (mb == NULL) 142099a0e58SBosko Milekic goto failed; 143099a0e58SBosko Milekic mb->m_len = 0; 144099a0e58SBosko Milekic if (cur == NULL) 145099a0e58SBosko Milekic top = mb; 146099a0e58SBosko Milekic else 147099a0e58SBosko Milekic cur->m_next = mb; 148099a0e58SBosko Milekic } 149099a0e58SBosko Milekic 150099a0e58SBosko Milekic if (mtail != NULL) 151099a0e58SBosko Milekic mtail->m_next = top; 152099a0e58SBosko Milekic return top; 153099a0e58SBosko Milekic failed: 154099a0e58SBosko Milekic if (top != NULL) 155099a0e58SBosko Milekic m_freem(top); 156099a0e58SBosko Milekic return NULL; 157099a0e58SBosko Milekic } 158099a0e58SBosko Milekic 159099a0e58SBosko Milekic /* 160099a0e58SBosko Milekic * Free an entire chain of mbufs and associated external buffers, if 161099a0e58SBosko Milekic * applicable. 162099a0e58SBosko Milekic */ 163099a0e58SBosko Milekic void 164099a0e58SBosko Milekic m_freem(struct mbuf *mb) 165099a0e58SBosko Milekic { 166099a0e58SBosko Milekic 167099a0e58SBosko Milekic while (mb != NULL) 168099a0e58SBosko Milekic mb = m_free(mb); 169099a0e58SBosko Milekic } 170099a0e58SBosko Milekic 171099a0e58SBosko Milekic /*- 172099a0e58SBosko Milekic * Configure a provided mbuf to refer to the provided external storage 173099a0e58SBosko Milekic * buffer and setup a reference count for said buffer. If the setting 174099a0e58SBosko Milekic * up of the reference count fails, the M_EXT bit will not be set. If 175099a0e58SBosko Milekic * successfull, the M_EXT bit is set in the mbuf's flags. 176099a0e58SBosko Milekic * 177099a0e58SBosko Milekic * Arguments: 178099a0e58SBosko Milekic * mb The existing mbuf to which to attach the provided buffer. 179099a0e58SBosko Milekic * buf The address of the provided external storage buffer. 180099a0e58SBosko Milekic * size The size of the provided buffer. 181099a0e58SBosko Milekic * freef A pointer to a routine that is responsible for freeing the 182099a0e58SBosko Milekic * provided external storage buffer. 183099a0e58SBosko Milekic * args A pointer to an argument structure (of any type) to be passed 184099a0e58SBosko Milekic * to the provided freef routine (may be NULL). 185099a0e58SBosko Milekic * flags Any other flags to be passed to the provided mbuf. 186099a0e58SBosko Milekic * type The type that the external storage buffer should be 187099a0e58SBosko Milekic * labeled with. 188099a0e58SBosko Milekic * 189099a0e58SBosko Milekic * Returns: 190099a0e58SBosko Milekic * Nothing. 191099a0e58SBosko Milekic */ 192099a0e58SBosko Milekic void 193099a0e58SBosko Milekic m_extadd(struct mbuf *mb, caddr_t buf, u_int size, 194099a0e58SBosko Milekic void (*freef)(void *, void *), void *args, int flags, int type) 195099a0e58SBosko Milekic { 196099a0e58SBosko Milekic u_int *ref_cnt = NULL; 197099a0e58SBosko Milekic 198099a0e58SBosko Milekic /* XXX Shouldn't be adding EXT_CLUSTER with this API */ 199099a0e58SBosko Milekic if (type == EXT_CLUSTER) 200099a0e58SBosko Milekic ref_cnt = (u_int *)uma_find_refcnt(zone_clust, 201099a0e58SBosko Milekic mb->m_ext.ext_buf); 202099a0e58SBosko Milekic else if (type == EXT_EXTREF) 203099a0e58SBosko Milekic ref_cnt = mb->m_ext.ref_cnt; 204099a0e58SBosko Milekic mb->m_ext.ref_cnt = (ref_cnt == NULL) ? 205099a0e58SBosko Milekic malloc(sizeof(u_int), M_MBUF, M_NOWAIT) : (u_int *)ref_cnt; 206099a0e58SBosko Milekic if (mb->m_ext.ref_cnt != NULL) { 207099a0e58SBosko Milekic *(mb->m_ext.ref_cnt) = 1; 208099a0e58SBosko Milekic mb->m_flags |= (M_EXT | flags); 209099a0e58SBosko Milekic mb->m_ext.ext_buf = buf; 210099a0e58SBosko Milekic mb->m_data = mb->m_ext.ext_buf; 211099a0e58SBosko Milekic mb->m_ext.ext_size = size; 212099a0e58SBosko Milekic mb->m_ext.ext_free = freef; 213099a0e58SBosko Milekic mb->m_ext.ext_args = args; 214099a0e58SBosko Milekic mb->m_ext.ext_type = type; 215099a0e58SBosko Milekic } 216099a0e58SBosko Milekic } 217099a0e58SBosko Milekic 218099a0e58SBosko Milekic /* 219099a0e58SBosko Milekic * Non-directly-exported function to clean up after mbufs with M_EXT 220099a0e58SBosko Milekic * storage attached to them if the reference count hits 0. 221099a0e58SBosko Milekic */ 222099a0e58SBosko Milekic void 223099a0e58SBosko Milekic mb_free_ext(struct mbuf *m) 224099a0e58SBosko Milekic { 225b5b2ea9aSBosko Milekic u_int cnt; 226099a0e58SBosko Milekic 227b5b2ea9aSBosko Milekic /* 228b5b2ea9aSBosko Milekic * This is tricky. We need to make sure to decrement the 229b5b2ea9aSBosko Milekic * refcount in a safe way but to also clean up if we're the 230b5b2ea9aSBosko Milekic * last reference. This method seems to do it without race. 231b5b2ea9aSBosko Milekic */ 232b5b2ea9aSBosko Milekic do { 233b5b2ea9aSBosko Milekic cnt = *(m->m_ext.ref_cnt); 234b5b2ea9aSBosko Milekic if (atomic_cmpset_int(m->m_ext.ref_cnt, cnt, cnt - 1)) { 235b5b2ea9aSBosko Milekic if (cnt == 1) { 236b5b2ea9aSBosko Milekic /* 237b5b2ea9aSBosko Milekic * Do the free, should be safe. 238b5b2ea9aSBosko Milekic */ 239099a0e58SBosko Milekic if (m->m_ext.ext_type == EXT_PACKET) { 240099a0e58SBosko Milekic uma_zfree(zone_pack, m); 24196e12413SBosko Milekic return; 242099a0e58SBosko Milekic } else if (m->m_ext.ext_type == EXT_CLUSTER) { 243099a0e58SBosko Milekic uma_zfree(zone_clust, m->m_ext.ext_buf); 244099a0e58SBosko Milekic m->m_ext.ext_buf = NULL; 245099a0e58SBosko Milekic } else { 246099a0e58SBosko Milekic (*(m->m_ext.ext_free))(m->m_ext.ext_buf, 247099a0e58SBosko Milekic m->m_ext.ext_args); 248099a0e58SBosko Milekic if (m->m_ext.ext_type != EXT_EXTREF) 249099a0e58SBosko Milekic free(m->m_ext.ref_cnt, M_MBUF); 25096e12413SBosko Milekic m->m_ext.ext_buf = NULL; 251099a0e58SBosko Milekic } 252099a0e58SBosko Milekic } 253b5b2ea9aSBosko Milekic /* Decrement (and potentially free) done, safely. */ 254b5b2ea9aSBosko Milekic break; 255b5b2ea9aSBosko Milekic } 256b5b2ea9aSBosko Milekic } while (1); 25796e12413SBosko Milekic uma_zfree(zone_mbuf, m); 258b5b2ea9aSBosko Milekic } 259099a0e58SBosko Milekic 260099a0e58SBosko Milekic /* 2619967cafcSSam Leffler * "Move" mbuf pkthdr from "from" to "to". 262e37b1fcdSRobert Watson * "from" must have M_PKTHDR set, and "to" must be empty. 263e37b1fcdSRobert Watson */ 264e37b1fcdSRobert Watson void 2659967cafcSSam Leffler m_move_pkthdr(struct mbuf *to, struct mbuf *from) 266e37b1fcdSRobert Watson { 267e37b1fcdSRobert Watson 268e37b1fcdSRobert Watson #if 0 2699967cafcSSam Leffler /* see below for why these are not enabled */ 270fe584538SDag-Erling Smørgrav M_ASSERTPKTHDR(to); 271225bff6fSRobert Watson /* Note: with MAC, this may not be a good assertion. */ 2729967cafcSSam Leffler KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), 2739967cafcSSam Leffler ("m_move_pkthdr: to has tags")); 274e37b1fcdSRobert Watson #endif 2759967cafcSSam Leffler KASSERT((to->m_flags & M_EXT) == 0, ("m_move_pkthdr: to has cluster")); 276e32a5b94SRobert Watson #ifdef MAC 277225bff6fSRobert Watson /* 278225bff6fSRobert Watson * XXXMAC: It could be this should also occur for non-MAC? 279225bff6fSRobert Watson */ 280e32a5b94SRobert Watson if (to->m_flags & M_PKTHDR) 281225bff6fSRobert Watson m_tag_delete_chain(to, NULL); 282e32a5b94SRobert Watson #endif 283e37b1fcdSRobert Watson to->m_flags = from->m_flags & M_COPYFLAGS; 2849967cafcSSam Leffler to->m_data = to->m_pktdat; 2859967cafcSSam Leffler to->m_pkthdr = from->m_pkthdr; /* especially tags */ 2869967cafcSSam Leffler SLIST_INIT(&from->m_pkthdr.tags); /* purge tags from src */ 2879967cafcSSam Leffler from->m_flags &= ~M_PKTHDR; 2889967cafcSSam Leffler } 2899967cafcSSam Leffler 2909967cafcSSam Leffler /* 2919967cafcSSam Leffler * Duplicate "from"'s mbuf pkthdr in "to". 2929967cafcSSam Leffler * "from" must have M_PKTHDR set, and "to" must be empty. 2939967cafcSSam Leffler * In particular, this does a deep copy of the packet tags. 2949967cafcSSam Leffler */ 2959967cafcSSam Leffler int 2969967cafcSSam Leffler m_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how) 2979967cafcSSam Leffler { 2989967cafcSSam Leffler 2999967cafcSSam Leffler #if 0 3009967cafcSSam Leffler /* 3019967cafcSSam Leffler * The mbuf allocator only initializes the pkthdr 3029967cafcSSam Leffler * when the mbuf is allocated with MGETHDR. Many users 3039967cafcSSam Leffler * (e.g. m_copy*, m_prepend) use MGET and then 3049967cafcSSam Leffler * smash the pkthdr as needed causing these 3059967cafcSSam Leffler * assertions to trip. For now just disable them. 3069967cafcSSam Leffler */ 307fe584538SDag-Erling Smørgrav M_ASSERTPKTHDR(to); 308225bff6fSRobert Watson /* Note: with MAC, this may not be a good assertion. */ 3099967cafcSSam Leffler KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), ("m_dup_pkthdr: to has tags")); 3109967cafcSSam Leffler #endif 311063d8114SAlfred Perlstein MBUF_CHECKSLEEP(how); 3129967cafcSSam Leffler #ifdef MAC 3139967cafcSSam Leffler if (to->m_flags & M_PKTHDR) 314225bff6fSRobert Watson m_tag_delete_chain(to, NULL); 3159967cafcSSam Leffler #endif 316df8c7fc9SMike Silbersack to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT); 317df8c7fc9SMike Silbersack if ((to->m_flags & M_EXT) == 0) 3189967cafcSSam Leffler to->m_data = to->m_pktdat; 319e37b1fcdSRobert Watson to->m_pkthdr = from->m_pkthdr; 3209967cafcSSam Leffler SLIST_INIT(&to->m_pkthdr.tags); 321aa65d9f5SRobert Watson return (m_tag_copy_chain(to, from, MBTOM(how))); 322e37b1fcdSRobert Watson } 323e37b1fcdSRobert Watson 324e37b1fcdSRobert Watson /* 325df8bae1dSRodney W. Grimes * Lesser-used path for M_PREPEND: 326df8bae1dSRodney W. Grimes * allocate new mbuf to prepend to chain, 327df8bae1dSRodney W. Grimes * copy junk along. 328df8bae1dSRodney W. Grimes */ 329df8bae1dSRodney W. Grimes struct mbuf * 330122a814aSBosko Milekic m_prepend(struct mbuf *m, int len, int how) 331df8bae1dSRodney W. Grimes { 332df8bae1dSRodney W. Grimes struct mbuf *mn; 333df8bae1dSRodney W. Grimes 334063d8114SAlfred Perlstein MBUF_CHECKSLEEP(how); 335f8bf8e39SMike Silbersack if (m->m_flags & M_PKTHDR) 336f8bf8e39SMike Silbersack MGETHDR(mn, how, m->m_type); 337f8bf8e39SMike Silbersack else 338df8bae1dSRodney W. Grimes MGET(mn, how, m->m_type); 339122a814aSBosko Milekic if (mn == NULL) { 340df8bae1dSRodney W. Grimes m_freem(m); 341122a814aSBosko Milekic return (NULL); 342df8bae1dSRodney W. Grimes } 343225bff6fSRobert Watson if (m->m_flags & M_PKTHDR) 3449967cafcSSam Leffler M_MOVE_PKTHDR(mn, m); 345df8bae1dSRodney W. Grimes mn->m_next = m; 346df8bae1dSRodney W. Grimes m = mn; 347df8bae1dSRodney W. Grimes if (len < MHLEN) 348df8bae1dSRodney W. Grimes MH_ALIGN(m, len); 349df8bae1dSRodney W. Grimes m->m_len = len; 350df8bae1dSRodney W. Grimes return (m); 351df8bae1dSRodney W. Grimes } 352df8bae1dSRodney W. Grimes 353df8bae1dSRodney W. Grimes /* 354df8bae1dSRodney W. Grimes * Make a copy of an mbuf chain starting "off0" bytes from the beginning, 355df8bae1dSRodney W. Grimes * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf. 356a163d034SWarner Losh * The wait parameter is a choice of M_TRYWAIT/M_DONTWAIT from caller. 3571c38f2eaSArchie Cobbs * Note that the copy is read-only, because clusters are not copied, 3581c38f2eaSArchie Cobbs * only their reference counts are incremented. 359df8bae1dSRodney W. Grimes */ 360df8bae1dSRodney W. Grimes struct mbuf * 361122a814aSBosko Milekic m_copym(struct mbuf *m, int off0, int len, int wait) 362df8bae1dSRodney W. Grimes { 363122a814aSBosko Milekic struct mbuf *n, **np; 364122a814aSBosko Milekic int off = off0; 365df8bae1dSRodney W. Grimes struct mbuf *top; 366df8bae1dSRodney W. Grimes int copyhdr = 0; 367df8bae1dSRodney W. Grimes 368e0a653ddSAlfred Perlstein KASSERT(off >= 0, ("m_copym, negative off %d", off)); 369e0a653ddSAlfred Perlstein KASSERT(len >= 0, ("m_copym, negative len %d", len)); 370063d8114SAlfred Perlstein MBUF_CHECKSLEEP(wait); 371df8bae1dSRodney W. Grimes if (off == 0 && m->m_flags & M_PKTHDR) 372df8bae1dSRodney W. Grimes copyhdr = 1; 373df8bae1dSRodney W. Grimes while (off > 0) { 374e0a653ddSAlfred Perlstein KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain")); 375df8bae1dSRodney W. Grimes if (off < m->m_len) 376df8bae1dSRodney W. Grimes break; 377df8bae1dSRodney W. Grimes off -= m->m_len; 378df8bae1dSRodney W. Grimes m = m->m_next; 379df8bae1dSRodney W. Grimes } 380df8bae1dSRodney W. Grimes np = ⊤ 381df8bae1dSRodney W. Grimes top = 0; 382df8bae1dSRodney W. Grimes while (len > 0) { 383122a814aSBosko Milekic if (m == NULL) { 384e0a653ddSAlfred Perlstein KASSERT(len == M_COPYALL, 385e0a653ddSAlfred Perlstein ("m_copym, length > size of mbuf chain")); 386df8bae1dSRodney W. Grimes break; 387df8bae1dSRodney W. Grimes } 388f8bf8e39SMike Silbersack if (copyhdr) 389f8bf8e39SMike Silbersack MGETHDR(n, wait, m->m_type); 390f8bf8e39SMike Silbersack else 391df8bae1dSRodney W. Grimes MGET(n, wait, m->m_type); 392df8bae1dSRodney W. Grimes *np = n; 393122a814aSBosko Milekic if (n == NULL) 394df8bae1dSRodney W. Grimes goto nospace; 395df8bae1dSRodney W. Grimes if (copyhdr) { 3969967cafcSSam Leffler if (!m_dup_pkthdr(n, m, wait)) 3979967cafcSSam Leffler goto nospace; 398df8bae1dSRodney W. Grimes if (len == M_COPYALL) 399df8bae1dSRodney W. Grimes n->m_pkthdr.len -= off0; 400df8bae1dSRodney W. Grimes else 401df8bae1dSRodney W. Grimes n->m_pkthdr.len = len; 402df8bae1dSRodney W. Grimes copyhdr = 0; 403df8bae1dSRodney W. Grimes } 404df8bae1dSRodney W. Grimes n->m_len = min(len, m->m_len - off); 405df8bae1dSRodney W. Grimes if (m->m_flags & M_EXT) { 406df8bae1dSRodney W. Grimes n->m_data = m->m_data + off; 407df8bae1dSRodney W. Grimes n->m_ext = m->m_ext; 408df8bae1dSRodney W. Grimes n->m_flags |= M_EXT; 409a5c4836dSDavid Malone MEXT_ADD_REF(m); 410df8bae1dSRodney W. Grimes } else 411df8bae1dSRodney W. Grimes bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), 412bd395ae8SBosko Milekic (u_int)n->m_len); 413df8bae1dSRodney W. Grimes if (len != M_COPYALL) 414df8bae1dSRodney W. Grimes len -= n->m_len; 415df8bae1dSRodney W. Grimes off = 0; 416df8bae1dSRodney W. Grimes m = m->m_next; 417df8bae1dSRodney W. Grimes np = &n->m_next; 418df8bae1dSRodney W. Grimes } 41908442f8aSBosko Milekic if (top == NULL) 42008442f8aSBosko Milekic mbstat.m_mcfail++; /* XXX: No consistency. */ 42108442f8aSBosko Milekic 422df8bae1dSRodney W. Grimes return (top); 423df8bae1dSRodney W. Grimes nospace: 424df8bae1dSRodney W. Grimes m_freem(top); 42508442f8aSBosko Milekic mbstat.m_mcfail++; /* XXX: No consistency. */ 426122a814aSBosko Milekic return (NULL); 427df8bae1dSRodney W. Grimes } 428df8bae1dSRodney W. Grimes 429df8bae1dSRodney W. Grimes /* 4306a06dea0SGarrett Wollman * Copy an entire packet, including header (which must be present). 4316a06dea0SGarrett Wollman * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'. 4321c38f2eaSArchie Cobbs * Note that the copy is read-only, because clusters are not copied, 4331c38f2eaSArchie Cobbs * only their reference counts are incremented. 4345fe86675SLuigi Rizzo * Preserve alignment of the first mbuf so if the creator has left 4355fe86675SLuigi Rizzo * some room at the beginning (e.g. for inserting protocol headers) 4365fe86675SLuigi Rizzo * the copies still have the room available. 4376a06dea0SGarrett Wollman */ 4386a06dea0SGarrett Wollman struct mbuf * 439122a814aSBosko Milekic m_copypacket(struct mbuf *m, int how) 4406a06dea0SGarrett Wollman { 4416a06dea0SGarrett Wollman struct mbuf *top, *n, *o; 4426a06dea0SGarrett Wollman 443063d8114SAlfred Perlstein MBUF_CHECKSLEEP(how); 4446a06dea0SGarrett Wollman MGET(n, how, m->m_type); 4456a06dea0SGarrett Wollman top = n; 446122a814aSBosko Milekic if (n == NULL) 4476a06dea0SGarrett Wollman goto nospace; 4486a06dea0SGarrett Wollman 4499967cafcSSam Leffler if (!m_dup_pkthdr(n, m, how)) 4509967cafcSSam Leffler goto nospace; 4516a06dea0SGarrett Wollman n->m_len = m->m_len; 4526a06dea0SGarrett Wollman if (m->m_flags & M_EXT) { 4536a06dea0SGarrett Wollman n->m_data = m->m_data; 4546a06dea0SGarrett Wollman n->m_ext = m->m_ext; 4556a06dea0SGarrett Wollman n->m_flags |= M_EXT; 456a5c4836dSDavid Malone MEXT_ADD_REF(m); 4576a06dea0SGarrett Wollman } else { 4585fe86675SLuigi Rizzo n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat ); 4596a06dea0SGarrett Wollman bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 4606a06dea0SGarrett Wollman } 4616a06dea0SGarrett Wollman 4626a06dea0SGarrett Wollman m = m->m_next; 4636a06dea0SGarrett Wollman while (m) { 4646a06dea0SGarrett Wollman MGET(o, how, m->m_type); 465122a814aSBosko Milekic if (o == NULL) 4666a06dea0SGarrett Wollman goto nospace; 4676a06dea0SGarrett Wollman 4686a06dea0SGarrett Wollman n->m_next = o; 4696a06dea0SGarrett Wollman n = n->m_next; 4706a06dea0SGarrett Wollman 4716a06dea0SGarrett Wollman n->m_len = m->m_len; 4726a06dea0SGarrett Wollman if (m->m_flags & M_EXT) { 4736a06dea0SGarrett Wollman n->m_data = m->m_data; 4746a06dea0SGarrett Wollman n->m_ext = m->m_ext; 4756a06dea0SGarrett Wollman n->m_flags |= M_EXT; 476a5c4836dSDavid Malone MEXT_ADD_REF(m); 4776a06dea0SGarrett Wollman } else { 4786a06dea0SGarrett Wollman bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 4796a06dea0SGarrett Wollman } 4806a06dea0SGarrett Wollman 4816a06dea0SGarrett Wollman m = m->m_next; 4826a06dea0SGarrett Wollman } 4836a06dea0SGarrett Wollman return top; 4846a06dea0SGarrett Wollman nospace: 4856a06dea0SGarrett Wollman m_freem(top); 48608442f8aSBosko Milekic mbstat.m_mcfail++; /* XXX: No consistency. */ 487122a814aSBosko Milekic return (NULL); 4886a06dea0SGarrett Wollman } 4896a06dea0SGarrett Wollman 4906a06dea0SGarrett Wollman /* 491df8bae1dSRodney W. Grimes * Copy data from an mbuf chain starting "off" bytes from the beginning, 492df8bae1dSRodney W. Grimes * continuing for "len" bytes, into the indicated buffer. 493df8bae1dSRodney W. Grimes */ 49426f9a767SRodney W. Grimes void 495a8cfc0eeSJulian Elischer m_copydata(const struct mbuf *m, int off, int len, caddr_t cp) 496df8bae1dSRodney W. Grimes { 497bd395ae8SBosko Milekic u_int count; 498df8bae1dSRodney W. Grimes 499e0a653ddSAlfred Perlstein KASSERT(off >= 0, ("m_copydata, negative off %d", off)); 500e0a653ddSAlfred Perlstein KASSERT(len >= 0, ("m_copydata, negative len %d", len)); 501df8bae1dSRodney W. Grimes while (off > 0) { 502e0a653ddSAlfred Perlstein KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain")); 503df8bae1dSRodney W. Grimes if (off < m->m_len) 504df8bae1dSRodney W. Grimes break; 505df8bae1dSRodney W. Grimes off -= m->m_len; 506df8bae1dSRodney W. Grimes m = m->m_next; 507df8bae1dSRodney W. Grimes } 508df8bae1dSRodney W. Grimes while (len > 0) { 509e0a653ddSAlfred Perlstein KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain")); 510df8bae1dSRodney W. Grimes count = min(m->m_len - off, len); 511df8bae1dSRodney W. Grimes bcopy(mtod(m, caddr_t) + off, cp, count); 512df8bae1dSRodney W. Grimes len -= count; 513df8bae1dSRodney W. Grimes cp += count; 514df8bae1dSRodney W. Grimes off = 0; 515df8bae1dSRodney W. Grimes m = m->m_next; 516df8bae1dSRodney W. Grimes } 517df8bae1dSRodney W. Grimes } 518df8bae1dSRodney W. Grimes 519df8bae1dSRodney W. Grimes /* 5201c38f2eaSArchie Cobbs * Copy a packet header mbuf chain into a completely new chain, including 5211c38f2eaSArchie Cobbs * copying any mbuf clusters. Use this instead of m_copypacket() when 5221c38f2eaSArchie Cobbs * you need a writable copy of an mbuf chain. 5231c38f2eaSArchie Cobbs */ 5241c38f2eaSArchie Cobbs struct mbuf * 525122a814aSBosko Milekic m_dup(struct mbuf *m, int how) 5261c38f2eaSArchie Cobbs { 5271c38f2eaSArchie Cobbs struct mbuf **p, *top = NULL; 5281c38f2eaSArchie Cobbs int remain, moff, nsize; 5291c38f2eaSArchie Cobbs 530063d8114SAlfred Perlstein MBUF_CHECKSLEEP(how); 5311c38f2eaSArchie Cobbs /* Sanity check */ 5321c38f2eaSArchie Cobbs if (m == NULL) 533122a814aSBosko Milekic return (NULL); 534fe584538SDag-Erling Smørgrav M_ASSERTPKTHDR(m); 5351c38f2eaSArchie Cobbs 5361c38f2eaSArchie Cobbs /* While there's more data, get a new mbuf, tack it on, and fill it */ 5371c38f2eaSArchie Cobbs remain = m->m_pkthdr.len; 5381c38f2eaSArchie Cobbs moff = 0; 5391c38f2eaSArchie Cobbs p = ⊤ 5401c38f2eaSArchie Cobbs while (remain > 0 || top == NULL) { /* allow m->m_pkthdr.len == 0 */ 5411c38f2eaSArchie Cobbs struct mbuf *n; 5421c38f2eaSArchie Cobbs 5431c38f2eaSArchie Cobbs /* Get the next new mbuf */ 544099a0e58SBosko Milekic if (remain >= MINCLSIZE) { 545099a0e58SBosko Milekic n = m_getcl(how, m->m_type, 0); 546099a0e58SBosko Milekic nsize = MCLBYTES; 547099a0e58SBosko Milekic } else { 548099a0e58SBosko Milekic n = m_get(how, m->m_type); 549099a0e58SBosko Milekic nsize = MLEN; 550099a0e58SBosko Milekic } 5511c38f2eaSArchie Cobbs if (n == NULL) 5521c38f2eaSArchie Cobbs goto nospace; 553099a0e58SBosko Milekic 554099a0e58SBosko Milekic if (top == NULL) { /* First one, must be PKTHDR */ 555099a0e58SBosko Milekic if (!m_dup_pkthdr(n, m, how)) { 556099a0e58SBosko Milekic m_free(n); 5571c38f2eaSArchie Cobbs goto nospace; 5581c38f2eaSArchie Cobbs } 559099a0e58SBosko Milekic nsize = MHLEN; 5601c38f2eaSArchie Cobbs } 5611c38f2eaSArchie Cobbs n->m_len = 0; 5621c38f2eaSArchie Cobbs 5631c38f2eaSArchie Cobbs /* Link it into the new chain */ 5641c38f2eaSArchie Cobbs *p = n; 5651c38f2eaSArchie Cobbs p = &n->m_next; 5661c38f2eaSArchie Cobbs 5671c38f2eaSArchie Cobbs /* Copy data from original mbuf(s) into new mbuf */ 5681c38f2eaSArchie Cobbs while (n->m_len < nsize && m != NULL) { 5691c38f2eaSArchie Cobbs int chunk = min(nsize - n->m_len, m->m_len - moff); 5701c38f2eaSArchie Cobbs 5711c38f2eaSArchie Cobbs bcopy(m->m_data + moff, n->m_data + n->m_len, chunk); 5721c38f2eaSArchie Cobbs moff += chunk; 5731c38f2eaSArchie Cobbs n->m_len += chunk; 5741c38f2eaSArchie Cobbs remain -= chunk; 5751c38f2eaSArchie Cobbs if (moff == m->m_len) { 5761c38f2eaSArchie Cobbs m = m->m_next; 5771c38f2eaSArchie Cobbs moff = 0; 5781c38f2eaSArchie Cobbs } 5791c38f2eaSArchie Cobbs } 5801c38f2eaSArchie Cobbs 5811c38f2eaSArchie Cobbs /* Check correct total mbuf length */ 5821c38f2eaSArchie Cobbs KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL), 583a48740b6SDavid E. O'Brien ("%s: bogus m_pkthdr.len", __func__)); 5841c38f2eaSArchie Cobbs } 5851c38f2eaSArchie Cobbs return (top); 5861c38f2eaSArchie Cobbs 5871c38f2eaSArchie Cobbs nospace: 5881c38f2eaSArchie Cobbs m_freem(top); 58908442f8aSBosko Milekic mbstat.m_mcfail++; /* XXX: No consistency. */ 590122a814aSBosko Milekic return (NULL); 5911c38f2eaSArchie Cobbs } 5921c38f2eaSArchie Cobbs 5931c38f2eaSArchie Cobbs /* 594df8bae1dSRodney W. Grimes * Concatenate mbuf chain n to m. 595df8bae1dSRodney W. Grimes * Both chains must be of the same type (e.g. MT_DATA). 596df8bae1dSRodney W. Grimes * Any m_pkthdr is not updated. 597df8bae1dSRodney W. Grimes */ 59826f9a767SRodney W. Grimes void 599122a814aSBosko Milekic m_cat(struct mbuf *m, struct mbuf *n) 600df8bae1dSRodney W. Grimes { 601df8bae1dSRodney W. Grimes while (m->m_next) 602df8bae1dSRodney W. Grimes m = m->m_next; 603df8bae1dSRodney W. Grimes while (n) { 604df8bae1dSRodney W. Grimes if (m->m_flags & M_EXT || 605df8bae1dSRodney W. Grimes m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) { 606df8bae1dSRodney W. Grimes /* just join the two chains */ 607df8bae1dSRodney W. Grimes m->m_next = n; 608df8bae1dSRodney W. Grimes return; 609df8bae1dSRodney W. Grimes } 610df8bae1dSRodney W. Grimes /* splat the data from one into the other */ 611df8bae1dSRodney W. Grimes bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 612df8bae1dSRodney W. Grimes (u_int)n->m_len); 613df8bae1dSRodney W. Grimes m->m_len += n->m_len; 614df8bae1dSRodney W. Grimes n = m_free(n); 615df8bae1dSRodney W. Grimes } 616df8bae1dSRodney W. Grimes } 617df8bae1dSRodney W. Grimes 61826f9a767SRodney W. Grimes void 619122a814aSBosko Milekic m_adj(struct mbuf *mp, int req_len) 620df8bae1dSRodney W. Grimes { 621122a814aSBosko Milekic int len = req_len; 622122a814aSBosko Milekic struct mbuf *m; 623122a814aSBosko Milekic int count; 624df8bae1dSRodney W. Grimes 625df8bae1dSRodney W. Grimes if ((m = mp) == NULL) 626df8bae1dSRodney W. Grimes return; 627df8bae1dSRodney W. Grimes if (len >= 0) { 628df8bae1dSRodney W. Grimes /* 629df8bae1dSRodney W. Grimes * Trim from head. 630df8bae1dSRodney W. Grimes */ 631df8bae1dSRodney W. Grimes while (m != NULL && len > 0) { 632df8bae1dSRodney W. Grimes if (m->m_len <= len) { 633df8bae1dSRodney W. Grimes len -= m->m_len; 634df8bae1dSRodney W. Grimes m->m_len = 0; 635df8bae1dSRodney W. Grimes m = m->m_next; 636df8bae1dSRodney W. Grimes } else { 637df8bae1dSRodney W. Grimes m->m_len -= len; 638df8bae1dSRodney W. Grimes m->m_data += len; 639df8bae1dSRodney W. Grimes len = 0; 640df8bae1dSRodney W. Grimes } 641df8bae1dSRodney W. Grimes } 642df8bae1dSRodney W. Grimes m = mp; 643df8bae1dSRodney W. Grimes if (mp->m_flags & M_PKTHDR) 644df8bae1dSRodney W. Grimes m->m_pkthdr.len -= (req_len - len); 645df8bae1dSRodney W. Grimes } else { 646df8bae1dSRodney W. Grimes /* 647df8bae1dSRodney W. Grimes * Trim from tail. Scan the mbuf chain, 648df8bae1dSRodney W. Grimes * calculating its length and finding the last mbuf. 649df8bae1dSRodney W. Grimes * If the adjustment only affects this mbuf, then just 650df8bae1dSRodney W. Grimes * adjust and return. Otherwise, rescan and truncate 651df8bae1dSRodney W. Grimes * after the remaining size. 652df8bae1dSRodney W. Grimes */ 653df8bae1dSRodney W. Grimes len = -len; 654df8bae1dSRodney W. Grimes count = 0; 655df8bae1dSRodney W. Grimes for (;;) { 656df8bae1dSRodney W. Grimes count += m->m_len; 657df8bae1dSRodney W. Grimes if (m->m_next == (struct mbuf *)0) 658df8bae1dSRodney W. Grimes break; 659df8bae1dSRodney W. Grimes m = m->m_next; 660df8bae1dSRodney W. Grimes } 661df8bae1dSRodney W. Grimes if (m->m_len >= len) { 662df8bae1dSRodney W. Grimes m->m_len -= len; 663df8bae1dSRodney W. Grimes if (mp->m_flags & M_PKTHDR) 664df8bae1dSRodney W. Grimes mp->m_pkthdr.len -= len; 665df8bae1dSRodney W. Grimes return; 666df8bae1dSRodney W. Grimes } 667df8bae1dSRodney W. Grimes count -= len; 668df8bae1dSRodney W. Grimes if (count < 0) 669df8bae1dSRodney W. Grimes count = 0; 670df8bae1dSRodney W. Grimes /* 671df8bae1dSRodney W. Grimes * Correct length for chain is "count". 672df8bae1dSRodney W. Grimes * Find the mbuf with last data, adjust its length, 673df8bae1dSRodney W. Grimes * and toss data from remaining mbufs on chain. 674df8bae1dSRodney W. Grimes */ 675df8bae1dSRodney W. Grimes m = mp; 676df8bae1dSRodney W. Grimes if (m->m_flags & M_PKTHDR) 677df8bae1dSRodney W. Grimes m->m_pkthdr.len = count; 678df8bae1dSRodney W. Grimes for (; m; m = m->m_next) { 679df8bae1dSRodney W. Grimes if (m->m_len >= count) { 680df8bae1dSRodney W. Grimes m->m_len = count; 681df8bae1dSRodney W. Grimes break; 682df8bae1dSRodney W. Grimes } 683df8bae1dSRodney W. Grimes count -= m->m_len; 684df8bae1dSRodney W. Grimes } 685797f2d22SPoul-Henning Kamp while (m->m_next) 686797f2d22SPoul-Henning Kamp (m = m->m_next) ->m_len = 0; 687df8bae1dSRodney W. Grimes } 688df8bae1dSRodney W. Grimes } 689df8bae1dSRodney W. Grimes 690df8bae1dSRodney W. Grimes /* 691df8bae1dSRodney W. Grimes * Rearange an mbuf chain so that len bytes are contiguous 692df8bae1dSRodney W. Grimes * and in the data area of an mbuf (so that mtod and dtom 693df8bae1dSRodney W. Grimes * will work for a structure of size len). Returns the resulting 694df8bae1dSRodney W. Grimes * mbuf chain on success, frees it and returns null on failure. 695df8bae1dSRodney W. Grimes * If there is room, it will add up to max_protohdr-len extra bytes to the 696df8bae1dSRodney W. Grimes * contiguous region in an attempt to avoid being called next time. 697df8bae1dSRodney W. Grimes */ 698df8bae1dSRodney W. Grimes struct mbuf * 699122a814aSBosko Milekic m_pullup(struct mbuf *n, int len) 700df8bae1dSRodney W. Grimes { 701122a814aSBosko Milekic struct mbuf *m; 702122a814aSBosko Milekic int count; 703df8bae1dSRodney W. Grimes int space; 704df8bae1dSRodney W. Grimes 705df8bae1dSRodney W. Grimes /* 706df8bae1dSRodney W. Grimes * If first mbuf has no cluster, and has room for len bytes 707df8bae1dSRodney W. Grimes * without shifting current data, pullup into it, 708df8bae1dSRodney W. Grimes * otherwise allocate a new mbuf to prepend to the chain. 709df8bae1dSRodney W. Grimes */ 710df8bae1dSRodney W. Grimes if ((n->m_flags & M_EXT) == 0 && 711df8bae1dSRodney W. Grimes n->m_data + len < &n->m_dat[MLEN] && n->m_next) { 712df8bae1dSRodney W. Grimes if (n->m_len >= len) 713df8bae1dSRodney W. Grimes return (n); 714df8bae1dSRodney W. Grimes m = n; 715df8bae1dSRodney W. Grimes n = n->m_next; 716df8bae1dSRodney W. Grimes len -= m->m_len; 717df8bae1dSRodney W. Grimes } else { 718df8bae1dSRodney W. Grimes if (len > MHLEN) 719df8bae1dSRodney W. Grimes goto bad; 720a163d034SWarner Losh MGET(m, M_DONTWAIT, n->m_type); 721122a814aSBosko Milekic if (m == NULL) 722df8bae1dSRodney W. Grimes goto bad; 723df8bae1dSRodney W. Grimes m->m_len = 0; 7249967cafcSSam Leffler if (n->m_flags & M_PKTHDR) 7259967cafcSSam Leffler M_MOVE_PKTHDR(m, n); 726df8bae1dSRodney W. Grimes } 727df8bae1dSRodney W. Grimes space = &m->m_dat[MLEN] - (m->m_data + m->m_len); 728df8bae1dSRodney W. Grimes do { 729df8bae1dSRodney W. Grimes count = min(min(max(len, max_protohdr), space), n->m_len); 730df8bae1dSRodney W. Grimes bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 731bd395ae8SBosko Milekic (u_int)count); 732df8bae1dSRodney W. Grimes len -= count; 733df8bae1dSRodney W. Grimes m->m_len += count; 734df8bae1dSRodney W. Grimes n->m_len -= count; 735df8bae1dSRodney W. Grimes space -= count; 736df8bae1dSRodney W. Grimes if (n->m_len) 737df8bae1dSRodney W. Grimes n->m_data += count; 738df8bae1dSRodney W. Grimes else 739df8bae1dSRodney W. Grimes n = m_free(n); 740df8bae1dSRodney W. Grimes } while (len > 0 && n); 741df8bae1dSRodney W. Grimes if (len > 0) { 742df8bae1dSRodney W. Grimes (void) m_free(m); 743df8bae1dSRodney W. Grimes goto bad; 744df8bae1dSRodney W. Grimes } 745df8bae1dSRodney W. Grimes m->m_next = n; 746df8bae1dSRodney W. Grimes return (m); 747df8bae1dSRodney W. Grimes bad: 748df8bae1dSRodney W. Grimes m_freem(n); 74908442f8aSBosko Milekic mbstat.m_mpfail++; /* XXX: No consistency. */ 750122a814aSBosko Milekic return (NULL); 751df8bae1dSRodney W. Grimes } 752df8bae1dSRodney W. Grimes 753df8bae1dSRodney W. Grimes /* 754df8bae1dSRodney W. Grimes * Partition an mbuf chain in two pieces, returning the tail -- 755df8bae1dSRodney W. Grimes * all but the first len0 bytes. In case of failure, it returns NULL and 756df8bae1dSRodney W. Grimes * attempts to restore the chain to its original state. 75748d183faSArchie Cobbs * 75848d183faSArchie Cobbs * Note that the resulting mbufs might be read-only, because the new 75948d183faSArchie Cobbs * mbuf can end up sharing an mbuf cluster with the original mbuf if 76048d183faSArchie Cobbs * the "breaking point" happens to lie within a cluster mbuf. Use the 76148d183faSArchie Cobbs * M_WRITABLE() macro to check for this case. 762df8bae1dSRodney W. Grimes */ 763df8bae1dSRodney W. Grimes struct mbuf * 764122a814aSBosko Milekic m_split(struct mbuf *m0, int len0, int wait) 765df8bae1dSRodney W. Grimes { 766122a814aSBosko Milekic struct mbuf *m, *n; 767bd395ae8SBosko Milekic u_int len = len0, remain; 768df8bae1dSRodney W. Grimes 769063d8114SAlfred Perlstein MBUF_CHECKSLEEP(wait); 770df8bae1dSRodney W. Grimes for (m = m0; m && len > m->m_len; m = m->m_next) 771df8bae1dSRodney W. Grimes len -= m->m_len; 772122a814aSBosko Milekic if (m == NULL) 773122a814aSBosko Milekic return (NULL); 774df8bae1dSRodney W. Grimes remain = m->m_len - len; 775df8bae1dSRodney W. Grimes if (m0->m_flags & M_PKTHDR) { 776df8bae1dSRodney W. Grimes MGETHDR(n, wait, m0->m_type); 777122a814aSBosko Milekic if (n == NULL) 778122a814aSBosko Milekic return (NULL); 779df8bae1dSRodney W. Grimes n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif; 780df8bae1dSRodney W. Grimes n->m_pkthdr.len = m0->m_pkthdr.len - len0; 781df8bae1dSRodney W. Grimes m0->m_pkthdr.len = len0; 782df8bae1dSRodney W. Grimes if (m->m_flags & M_EXT) 783df8bae1dSRodney W. Grimes goto extpacket; 784df8bae1dSRodney W. Grimes if (remain > MHLEN) { 785df8bae1dSRodney W. Grimes /* m can't be the lead packet */ 786df8bae1dSRodney W. Grimes MH_ALIGN(n, 0); 787df8bae1dSRodney W. Grimes n->m_next = m_split(m, len, wait); 788122a814aSBosko Milekic if (n->m_next == NULL) { 789df8bae1dSRodney W. Grimes (void) m_free(n); 790122a814aSBosko Milekic return (NULL); 79140376987SJeffrey Hsu } else { 79240376987SJeffrey Hsu n->m_len = 0; 793df8bae1dSRodney W. Grimes return (n); 79440376987SJeffrey Hsu } 795df8bae1dSRodney W. Grimes } else 796df8bae1dSRodney W. Grimes MH_ALIGN(n, remain); 797df8bae1dSRodney W. Grimes } else if (remain == 0) { 798df8bae1dSRodney W. Grimes n = m->m_next; 799122a814aSBosko Milekic m->m_next = NULL; 800df8bae1dSRodney W. Grimes return (n); 801df8bae1dSRodney W. Grimes } else { 802df8bae1dSRodney W. Grimes MGET(n, wait, m->m_type); 803122a814aSBosko Milekic if (n == NULL) 804122a814aSBosko Milekic return (NULL); 805df8bae1dSRodney W. Grimes M_ALIGN(n, remain); 806df8bae1dSRodney W. Grimes } 807df8bae1dSRodney W. Grimes extpacket: 808df8bae1dSRodney W. Grimes if (m->m_flags & M_EXT) { 809df8bae1dSRodney W. Grimes n->m_flags |= M_EXT; 810df8bae1dSRodney W. Grimes n->m_ext = m->m_ext; 811a5c4836dSDavid Malone MEXT_ADD_REF(m); 812df8bae1dSRodney W. Grimes n->m_data = m->m_data + len; 813df8bae1dSRodney W. Grimes } else { 814df8bae1dSRodney W. Grimes bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain); 815df8bae1dSRodney W. Grimes } 816df8bae1dSRodney W. Grimes n->m_len = remain; 817df8bae1dSRodney W. Grimes m->m_len = len; 818df8bae1dSRodney W. Grimes n->m_next = m->m_next; 819122a814aSBosko Milekic m->m_next = NULL; 820df8bae1dSRodney W. Grimes return (n); 821df8bae1dSRodney W. Grimes } 822df8bae1dSRodney W. Grimes /* 823df8bae1dSRodney W. Grimes * Routine to copy from device local memory into mbufs. 824f5eece3fSBosko Milekic * Note that `off' argument is offset into first mbuf of target chain from 825f5eece3fSBosko Milekic * which to begin copying the data to. 826df8bae1dSRodney W. Grimes */ 827df8bae1dSRodney W. Grimes struct mbuf * 828f5eece3fSBosko Milekic m_devget(char *buf, int totlen, int off, struct ifnet *ifp, 829122a814aSBosko Milekic void (*copy)(char *from, caddr_t to, u_int len)) 830df8bae1dSRodney W. Grimes { 831122a814aSBosko Milekic struct mbuf *m; 832099a0e58SBosko Milekic struct mbuf *top = NULL, **mp = ⊤ 833f5eece3fSBosko Milekic int len; 834df8bae1dSRodney W. Grimes 835f5eece3fSBosko Milekic if (off < 0 || off > MHLEN) 836f5eece3fSBosko Milekic return (NULL); 837f5eece3fSBosko Milekic 838df8bae1dSRodney W. Grimes while (totlen > 0) { 839099a0e58SBosko Milekic if (top == NULL) { /* First one, must be PKTHDR */ 840f5eece3fSBosko Milekic if (totlen + off >= MINCLSIZE) { 841099a0e58SBosko Milekic m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 842f5eece3fSBosko Milekic len = MCLBYTES; 843df8bae1dSRodney W. Grimes } else { 844099a0e58SBosko Milekic m = m_gethdr(M_DONTWAIT, MT_DATA); 845099a0e58SBosko Milekic len = MHLEN; 846099a0e58SBosko Milekic 847099a0e58SBosko Milekic /* Place initial small packet/header at end of mbuf */ 848099a0e58SBosko Milekic if (m && totlen + off + max_linkhdr <= MLEN) { 849df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 850f5eece3fSBosko Milekic len -= max_linkhdr; 851df8bae1dSRodney W. Grimes } 852f5eece3fSBosko Milekic } 853099a0e58SBosko Milekic if (m == NULL) 854099a0e58SBosko Milekic return NULL; 855099a0e58SBosko Milekic m->m_pkthdr.rcvif = ifp; 856099a0e58SBosko Milekic m->m_pkthdr.len = totlen; 857099a0e58SBosko Milekic } else { 858099a0e58SBosko Milekic if (totlen + off >= MINCLSIZE) { 859099a0e58SBosko Milekic m = m_getcl(M_DONTWAIT, MT_DATA, 0); 860099a0e58SBosko Milekic len = MCLBYTES; 861099a0e58SBosko Milekic } else { 862099a0e58SBosko Milekic m = m_get(M_DONTWAIT, MT_DATA); 863099a0e58SBosko Milekic len = MLEN; 864099a0e58SBosko Milekic } 865099a0e58SBosko Milekic if (m == NULL) { 866099a0e58SBosko Milekic m_freem(top); 867099a0e58SBosko Milekic return NULL; 868099a0e58SBosko Milekic } 869099a0e58SBosko Milekic } 870f5eece3fSBosko Milekic if (off) { 871f5eece3fSBosko Milekic m->m_data += off; 872f5eece3fSBosko Milekic len -= off; 873f5eece3fSBosko Milekic off = 0; 874f5eece3fSBosko Milekic } 875f5eece3fSBosko Milekic m->m_len = len = min(totlen, len); 876df8bae1dSRodney W. Grimes if (copy) 877bd395ae8SBosko Milekic copy(buf, mtod(m, caddr_t), (u_int)len); 878df8bae1dSRodney W. Grimes else 879bd395ae8SBosko Milekic bcopy(buf, mtod(m, caddr_t), (u_int)len); 880f5eece3fSBosko Milekic buf += len; 881df8bae1dSRodney W. Grimes *mp = m; 882df8bae1dSRodney W. Grimes mp = &m->m_next; 883df8bae1dSRodney W. Grimes totlen -= len; 884df8bae1dSRodney W. Grimes } 885df8bae1dSRodney W. Grimes return (top); 886df8bae1dSRodney W. Grimes } 887c5789ba3SPoul-Henning Kamp 888c5789ba3SPoul-Henning Kamp /* 889c5789ba3SPoul-Henning Kamp * Copy data from a buffer back into the indicated mbuf chain, 890c5789ba3SPoul-Henning Kamp * starting "off" bytes from the beginning, extending the mbuf 891c5789ba3SPoul-Henning Kamp * chain if necessary. 892c5789ba3SPoul-Henning Kamp */ 893c5789ba3SPoul-Henning Kamp void 89424665342SLuigi Rizzo m_copyback(struct mbuf *m0, int off, int len, c_caddr_t cp) 895c5789ba3SPoul-Henning Kamp { 896122a814aSBosko Milekic int mlen; 897122a814aSBosko Milekic struct mbuf *m = m0, *n; 898c5789ba3SPoul-Henning Kamp int totlen = 0; 899c5789ba3SPoul-Henning Kamp 900122a814aSBosko Milekic if (m0 == NULL) 901c5789ba3SPoul-Henning Kamp return; 902c5789ba3SPoul-Henning Kamp while (off > (mlen = m->m_len)) { 903c5789ba3SPoul-Henning Kamp off -= mlen; 904c5789ba3SPoul-Henning Kamp totlen += mlen; 905122a814aSBosko Milekic if (m->m_next == NULL) { 906099a0e58SBosko Milekic n = m_get(M_DONTWAIT, m->m_type); 907122a814aSBosko Milekic if (n == NULL) 908c5789ba3SPoul-Henning Kamp goto out; 909099a0e58SBosko Milekic bzero(mtod(n, caddr_t), MLEN); 910c5789ba3SPoul-Henning Kamp n->m_len = min(MLEN, len + off); 911c5789ba3SPoul-Henning Kamp m->m_next = n; 912c5789ba3SPoul-Henning Kamp } 913c5789ba3SPoul-Henning Kamp m = m->m_next; 914c5789ba3SPoul-Henning Kamp } 915c5789ba3SPoul-Henning Kamp while (len > 0) { 916c5789ba3SPoul-Henning Kamp mlen = min (m->m_len - off, len); 917bd395ae8SBosko Milekic bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen); 918c5789ba3SPoul-Henning Kamp cp += mlen; 919c5789ba3SPoul-Henning Kamp len -= mlen; 920c5789ba3SPoul-Henning Kamp mlen += off; 921c5789ba3SPoul-Henning Kamp off = 0; 922c5789ba3SPoul-Henning Kamp totlen += mlen; 923c5789ba3SPoul-Henning Kamp if (len == 0) 924c5789ba3SPoul-Henning Kamp break; 925122a814aSBosko Milekic if (m->m_next == NULL) { 926a163d034SWarner Losh n = m_get(M_DONTWAIT, m->m_type); 927122a814aSBosko Milekic if (n == NULL) 928c5789ba3SPoul-Henning Kamp break; 929c5789ba3SPoul-Henning Kamp n->m_len = min(MLEN, len); 930c5789ba3SPoul-Henning Kamp m->m_next = n; 931c5789ba3SPoul-Henning Kamp } 932c5789ba3SPoul-Henning Kamp m = m->m_next; 933c5789ba3SPoul-Henning Kamp } 934c5789ba3SPoul-Henning Kamp out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) 935c5789ba3SPoul-Henning Kamp m->m_pkthdr.len = totlen; 936c5789ba3SPoul-Henning Kamp } 937ce4a64f7SPoul-Henning Kamp 93837621fd5SBruce M Simpson /* 93937621fd5SBruce M Simpson * Apply function f to the data in an mbuf chain starting "off" bytes from 94037621fd5SBruce M Simpson * the beginning, continuing for "len" bytes. 94137621fd5SBruce M Simpson */ 94237621fd5SBruce M Simpson int 94337621fd5SBruce M Simpson m_apply(struct mbuf *m, int off, int len, 94454065297SBruce M Simpson int (*f)(void *, void *, u_int), void *arg) 94537621fd5SBruce M Simpson { 94654065297SBruce M Simpson u_int count; 94737621fd5SBruce M Simpson int rval; 94837621fd5SBruce M Simpson 94937621fd5SBruce M Simpson KASSERT(off >= 0, ("m_apply, negative off %d", off)); 95037621fd5SBruce M Simpson KASSERT(len >= 0, ("m_apply, negative len %d", len)); 95137621fd5SBruce M Simpson while (off > 0) { 95237621fd5SBruce M Simpson KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain")); 95337621fd5SBruce M Simpson if (off < m->m_len) 95437621fd5SBruce M Simpson break; 95537621fd5SBruce M Simpson off -= m->m_len; 95637621fd5SBruce M Simpson m = m->m_next; 95737621fd5SBruce M Simpson } 95837621fd5SBruce M Simpson while (len > 0) { 95937621fd5SBruce M Simpson KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain")); 96037621fd5SBruce M Simpson count = min(m->m_len - off, len); 96137621fd5SBruce M Simpson rval = (*f)(arg, mtod(m, caddr_t) + off, count); 96237621fd5SBruce M Simpson if (rval) 96337621fd5SBruce M Simpson return (rval); 96437621fd5SBruce M Simpson len -= count; 96537621fd5SBruce M Simpson off = 0; 96637621fd5SBruce M Simpson m = m->m_next; 96737621fd5SBruce M Simpson } 96837621fd5SBruce M Simpson return (0); 96937621fd5SBruce M Simpson } 97037621fd5SBruce M Simpson 97137621fd5SBruce M Simpson /* 97237621fd5SBruce M Simpson * Return a pointer to mbuf/offset of location in mbuf chain. 97337621fd5SBruce M Simpson */ 97437621fd5SBruce M Simpson struct mbuf * 97537621fd5SBruce M Simpson m_getptr(struct mbuf *m, int loc, int *off) 97637621fd5SBruce M Simpson { 97737621fd5SBruce M Simpson 97837621fd5SBruce M Simpson while (loc >= 0) { 97954065297SBruce M Simpson /* Normal end of search. */ 98037621fd5SBruce M Simpson if (m->m_len > loc) { 98137621fd5SBruce M Simpson *off = loc; 98237621fd5SBruce M Simpson return (m); 98337621fd5SBruce M Simpson } else { 98437621fd5SBruce M Simpson loc -= m->m_len; 98537621fd5SBruce M Simpson if (m->m_next == NULL) { 98637621fd5SBruce M Simpson if (loc == 0) { 98754065297SBruce M Simpson /* Point at the end of valid data. */ 98837621fd5SBruce M Simpson *off = m->m_len; 98937621fd5SBruce M Simpson return (m); 99054065297SBruce M Simpson } 99137621fd5SBruce M Simpson return (NULL); 99254065297SBruce M Simpson } 99337621fd5SBruce M Simpson m = m->m_next; 99437621fd5SBruce M Simpson } 99537621fd5SBruce M Simpson } 99637621fd5SBruce M Simpson return (NULL); 99737621fd5SBruce M Simpson } 99837621fd5SBruce M Simpson 999ce4a64f7SPoul-Henning Kamp void 1000ce4a64f7SPoul-Henning Kamp m_print(const struct mbuf *m) 1001ce4a64f7SPoul-Henning Kamp { 1002ce4a64f7SPoul-Henning Kamp int len; 10036357e7b5SEivind Eklund const struct mbuf *m2; 1004ce4a64f7SPoul-Henning Kamp 1005ce4a64f7SPoul-Henning Kamp len = m->m_pkthdr.len; 1006ce4a64f7SPoul-Henning Kamp m2 = m; 1007ce4a64f7SPoul-Henning Kamp while (len) { 1008ce4a64f7SPoul-Henning Kamp printf("%p %*D\n", m2, m2->m_len, (u_char *)m2->m_data, "-"); 1009ce4a64f7SPoul-Henning Kamp len -= m2->m_len; 1010ce4a64f7SPoul-Henning Kamp m2 = m2->m_next; 1011ce4a64f7SPoul-Henning Kamp } 1012ce4a64f7SPoul-Henning Kamp return; 1013ce4a64f7SPoul-Henning Kamp } 10143f2e06c5SPoul-Henning Kamp 1015bd395ae8SBosko Milekic u_int 10163f2e06c5SPoul-Henning Kamp m_fixhdr(struct mbuf *m0) 10173f2e06c5SPoul-Henning Kamp { 1018bd395ae8SBosko Milekic u_int len; 10193f2e06c5SPoul-Henning Kamp 1020ac6e585dSPoul-Henning Kamp len = m_length(m0, NULL); 10213f2e06c5SPoul-Henning Kamp m0->m_pkthdr.len = len; 1022ac6e585dSPoul-Henning Kamp return (len); 1023ac6e585dSPoul-Henning Kamp } 1024ac6e585dSPoul-Henning Kamp 1025bd395ae8SBosko Milekic u_int 1026ac6e585dSPoul-Henning Kamp m_length(struct mbuf *m0, struct mbuf **last) 1027ac6e585dSPoul-Henning Kamp { 1028ac6e585dSPoul-Henning Kamp struct mbuf *m; 1029bd395ae8SBosko Milekic u_int len; 1030ac6e585dSPoul-Henning Kamp 1031ac6e585dSPoul-Henning Kamp len = 0; 1032ac6e585dSPoul-Henning Kamp for (m = m0; m != NULL; m = m->m_next) { 1033ac6e585dSPoul-Henning Kamp len += m->m_len; 1034ac6e585dSPoul-Henning Kamp if (m->m_next == NULL) 1035ac6e585dSPoul-Henning Kamp break; 1036ac6e585dSPoul-Henning Kamp } 1037ac6e585dSPoul-Henning Kamp if (last != NULL) 1038ac6e585dSPoul-Henning Kamp *last = m; 1039ac6e585dSPoul-Henning Kamp return (len); 10403f2e06c5SPoul-Henning Kamp } 104155e9f80dSMike Silbersack 104255e9f80dSMike Silbersack /* 104355e9f80dSMike Silbersack * Defragment a mbuf chain, returning the shortest possible 104455e9f80dSMike Silbersack * chain of mbufs and clusters. If allocation fails and 104555e9f80dSMike Silbersack * this cannot be completed, NULL will be returned, but 104655e9f80dSMike Silbersack * the passed in chain will be unchanged. Upon success, 104755e9f80dSMike Silbersack * the original chain will be freed, and the new chain 104855e9f80dSMike Silbersack * will be returned. 104955e9f80dSMike Silbersack * 105055e9f80dSMike Silbersack * If a non-packet header is passed in, the original 105155e9f80dSMike Silbersack * mbuf (chain?) will be returned unharmed. 105255e9f80dSMike Silbersack */ 105355e9f80dSMike Silbersack struct mbuf * 105455e9f80dSMike Silbersack m_defrag(struct mbuf *m0, int how) 105555e9f80dSMike Silbersack { 105655e9f80dSMike Silbersack struct mbuf *m_new = NULL, *m_final = NULL; 105755e9f80dSMike Silbersack int progress = 0, length; 105855e9f80dSMike Silbersack 1059063d8114SAlfred Perlstein MBUF_CHECKSLEEP(how); 106055e9f80dSMike Silbersack if (!(m0->m_flags & M_PKTHDR)) 106155e9f80dSMike Silbersack return (m0); 106255e9f80dSMike Silbersack 1063f8bf8e39SMike Silbersack m_fixhdr(m0); /* Needed sanity check */ 1064f8bf8e39SMike Silbersack 1065352d050eSMike Silbersack #ifdef MBUF_STRESS_TEST 1066352d050eSMike Silbersack if (m_defragrandomfailures) { 1067352d050eSMike Silbersack int temp = arc4random() & 0xff; 1068352d050eSMike Silbersack if (temp == 0xba) 1069352d050eSMike Silbersack goto nospace; 1070352d050eSMike Silbersack } 1071352d050eSMike Silbersack #endif 107255e9f80dSMike Silbersack 107355e9f80dSMike Silbersack if (m0->m_pkthdr.len > MHLEN) 107455e9f80dSMike Silbersack m_final = m_getcl(how, MT_DATA, M_PKTHDR); 107555e9f80dSMike Silbersack else 107655e9f80dSMike Silbersack m_final = m_gethdr(how, MT_DATA); 107755e9f80dSMike Silbersack 107855e9f80dSMike Silbersack if (m_final == NULL) 107955e9f80dSMike Silbersack goto nospace; 108055e9f80dSMike Silbersack 1081a89ec05eSPeter Wemm if (m_dup_pkthdr(m_final, m0, how) == 0) 108255e9f80dSMike Silbersack goto nospace; 108355e9f80dSMike Silbersack 108455e9f80dSMike Silbersack m_new = m_final; 108555e9f80dSMike Silbersack 108655e9f80dSMike Silbersack while (progress < m0->m_pkthdr.len) { 108755e9f80dSMike Silbersack length = m0->m_pkthdr.len - progress; 108855e9f80dSMike Silbersack if (length > MCLBYTES) 108955e9f80dSMike Silbersack length = MCLBYTES; 109055e9f80dSMike Silbersack 109155e9f80dSMike Silbersack if (m_new == NULL) { 109255e9f80dSMike Silbersack if (length > MLEN) 109355e9f80dSMike Silbersack m_new = m_getcl(how, MT_DATA, 0); 109455e9f80dSMike Silbersack else 109555e9f80dSMike Silbersack m_new = m_get(how, MT_DATA); 109655e9f80dSMike Silbersack if (m_new == NULL) 109755e9f80dSMike Silbersack goto nospace; 109855e9f80dSMike Silbersack } 109955e9f80dSMike Silbersack 110055e9f80dSMike Silbersack m_copydata(m0, progress, length, mtod(m_new, caddr_t)); 110155e9f80dSMike Silbersack progress += length; 110255e9f80dSMike Silbersack m_new->m_len = length; 110355e9f80dSMike Silbersack if (m_new != m_final) 110455e9f80dSMike Silbersack m_cat(m_final, m_new); 110555e9f80dSMike Silbersack m_new = NULL; 110655e9f80dSMike Silbersack } 110751710a45SMike Silbersack #ifdef MBUF_STRESS_TEST 110855e9f80dSMike Silbersack if (m0->m_next == NULL) 110955e9f80dSMike Silbersack m_defraguseless++; 111051710a45SMike Silbersack #endif 111155e9f80dSMike Silbersack m_freem(m0); 111255e9f80dSMike Silbersack m0 = m_final; 111351710a45SMike Silbersack #ifdef MBUF_STRESS_TEST 111455e9f80dSMike Silbersack m_defragpackets++; 111555e9f80dSMike Silbersack m_defragbytes += m0->m_pkthdr.len; 111651710a45SMike Silbersack #endif 111755e9f80dSMike Silbersack return (m0); 111855e9f80dSMike Silbersack nospace: 111951710a45SMike Silbersack #ifdef MBUF_STRESS_TEST 112055e9f80dSMike Silbersack m_defragfailure++; 112151710a45SMike Silbersack #endif 112255e9f80dSMike Silbersack if (m_new) 112355e9f80dSMike Silbersack m_free(m_new); 112455e9f80dSMike Silbersack if (m_final) 112555e9f80dSMike Silbersack m_freem(m_final); 112655e9f80dSMike Silbersack return (NULL); 112755e9f80dSMike Silbersack } 11283390d476SMike Silbersack 11293390d476SMike Silbersack #ifdef MBUF_STRESS_TEST 11303390d476SMike Silbersack 11313390d476SMike Silbersack /* 11323390d476SMike Silbersack * Fragment an mbuf chain. There's no reason you'd ever want to do 11333390d476SMike Silbersack * this in normal usage, but it's great for stress testing various 11343390d476SMike Silbersack * mbuf consumers. 11353390d476SMike Silbersack * 11363390d476SMike Silbersack * If fragmentation is not possible, the original chain will be 11373390d476SMike Silbersack * returned. 11383390d476SMike Silbersack * 11393390d476SMike Silbersack * Possible length values: 11403390d476SMike Silbersack * 0 no fragmentation will occur 11413390d476SMike Silbersack * > 0 each fragment will be of the specified length 11423390d476SMike Silbersack * -1 each fragment will be the same random value in length 11433390d476SMike Silbersack * -2 each fragment's length will be entirely random 11443390d476SMike Silbersack * (Random values range from 1 to 256) 11453390d476SMike Silbersack */ 11463390d476SMike Silbersack struct mbuf * 11473390d476SMike Silbersack m_fragment(struct mbuf *m0, int how, int length) 11483390d476SMike Silbersack { 11493390d476SMike Silbersack struct mbuf *m_new = NULL, *m_final = NULL; 11503390d476SMike Silbersack int progress = 0; 11513390d476SMike Silbersack 11523390d476SMike Silbersack if (!(m0->m_flags & M_PKTHDR)) 11533390d476SMike Silbersack return (m0); 11543390d476SMike Silbersack 11553390d476SMike Silbersack if ((length == 0) || (length < -2)) 11563390d476SMike Silbersack return (m0); 11573390d476SMike Silbersack 11583390d476SMike Silbersack m_fixhdr(m0); /* Needed sanity check */ 11593390d476SMike Silbersack 11603390d476SMike Silbersack m_final = m_getcl(how, MT_DATA, M_PKTHDR); 11613390d476SMike Silbersack 11623390d476SMike Silbersack if (m_final == NULL) 11633390d476SMike Silbersack goto nospace; 11643390d476SMike Silbersack 11658dee2f67SMike Silbersack if (m_dup_pkthdr(m_final, m0, how) == 0) 11663390d476SMike Silbersack goto nospace; 11673390d476SMike Silbersack 11683390d476SMike Silbersack m_new = m_final; 11693390d476SMike Silbersack 11703390d476SMike Silbersack if (length == -1) 11713390d476SMike Silbersack length = 1 + (arc4random() & 255); 11723390d476SMike Silbersack 11733390d476SMike Silbersack while (progress < m0->m_pkthdr.len) { 11743390d476SMike Silbersack int fraglen; 11753390d476SMike Silbersack 11763390d476SMike Silbersack if (length > 0) 11773390d476SMike Silbersack fraglen = length; 11783390d476SMike Silbersack else 11793390d476SMike Silbersack fraglen = 1 + (arc4random() & 255); 11803390d476SMike Silbersack if (fraglen > m0->m_pkthdr.len - progress) 11813390d476SMike Silbersack fraglen = m0->m_pkthdr.len - progress; 11823390d476SMike Silbersack 11833390d476SMike Silbersack if (fraglen > MCLBYTES) 11843390d476SMike Silbersack fraglen = MCLBYTES; 11853390d476SMike Silbersack 11863390d476SMike Silbersack if (m_new == NULL) { 11873390d476SMike Silbersack m_new = m_getcl(how, MT_DATA, 0); 11883390d476SMike Silbersack if (m_new == NULL) 11893390d476SMike Silbersack goto nospace; 11903390d476SMike Silbersack } 11913390d476SMike Silbersack 11923390d476SMike Silbersack m_copydata(m0, progress, fraglen, mtod(m_new, caddr_t)); 11933390d476SMike Silbersack progress += fraglen; 11943390d476SMike Silbersack m_new->m_len = fraglen; 11953390d476SMike Silbersack if (m_new != m_final) 11963390d476SMike Silbersack m_cat(m_final, m_new); 11973390d476SMike Silbersack m_new = NULL; 11983390d476SMike Silbersack } 11993390d476SMike Silbersack m_freem(m0); 12003390d476SMike Silbersack m0 = m_final; 12013390d476SMike Silbersack return (m0); 12023390d476SMike Silbersack nospace: 12033390d476SMike Silbersack if (m_new) 12043390d476SMike Silbersack m_free(m_new); 12053390d476SMike Silbersack if (m_final) 12063390d476SMike Silbersack m_freem(m_final); 12073390d476SMike Silbersack /* Return the original chain on failure */ 12083390d476SMike Silbersack return (m0); 12093390d476SMike Silbersack } 12103390d476SMike Silbersack 12113390d476SMike Silbersack #endif 1212beb699c7SMike Silbersack 1213beb699c7SMike Silbersack struct mbuf * 1214beb699c7SMike Silbersack m_uiotombuf(struct uio *uio, int how, int len) 1215beb699c7SMike Silbersack { 1216beb699c7SMike Silbersack struct mbuf *m_new = NULL, *m_final = NULL; 1217beb699c7SMike Silbersack int progress = 0, error = 0, length, total; 1218beb699c7SMike Silbersack 1219beb699c7SMike Silbersack if (len > 0) 1220beb699c7SMike Silbersack total = min(uio->uio_resid, len); 1221beb699c7SMike Silbersack else 1222beb699c7SMike Silbersack total = uio->uio_resid; 1223beb699c7SMike Silbersack if (total > MHLEN) 1224beb699c7SMike Silbersack m_final = m_getcl(how, MT_DATA, M_PKTHDR); 1225beb699c7SMike Silbersack else 1226beb699c7SMike Silbersack m_final = m_gethdr(how, MT_DATA); 1227beb699c7SMike Silbersack if (m_final == NULL) 1228beb699c7SMike Silbersack goto nospace; 1229beb699c7SMike Silbersack m_new = m_final; 1230beb699c7SMike Silbersack while (progress < total) { 1231beb699c7SMike Silbersack length = total - progress; 1232beb699c7SMike Silbersack if (length > MCLBYTES) 1233beb699c7SMike Silbersack length = MCLBYTES; 1234beb699c7SMike Silbersack if (m_new == NULL) { 1235beb699c7SMike Silbersack if (length > MLEN) 1236beb699c7SMike Silbersack m_new = m_getcl(how, MT_DATA, 0); 1237beb699c7SMike Silbersack else 1238beb699c7SMike Silbersack m_new = m_get(how, MT_DATA); 1239beb699c7SMike Silbersack if (m_new == NULL) 1240beb699c7SMike Silbersack goto nospace; 1241beb699c7SMike Silbersack } 1242beb699c7SMike Silbersack error = uiomove(mtod(m_new, void *), length, uio); 1243beb699c7SMike Silbersack if (error) 1244beb699c7SMike Silbersack goto nospace; 1245beb699c7SMike Silbersack progress += length; 1246beb699c7SMike Silbersack m_new->m_len = length; 1247beb699c7SMike Silbersack if (m_new != m_final) 1248beb699c7SMike Silbersack m_cat(m_final, m_new); 1249beb699c7SMike Silbersack m_new = NULL; 1250beb699c7SMike Silbersack } 1251beb699c7SMike Silbersack m_fixhdr(m_final); 1252beb699c7SMike Silbersack return (m_final); 1253beb699c7SMike Silbersack nospace: 1254beb699c7SMike Silbersack if (m_new) 1255beb699c7SMike Silbersack m_free(m_new); 1256beb699c7SMike Silbersack if (m_final) 1257beb699c7SMike Silbersack m_freem(m_final); 1258beb699c7SMike Silbersack return (NULL); 1259beb699c7SMike Silbersack } 1260