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")); 108099a0e58SBosko Milekic 109099a0e58SBosko Milekic /* If m != NULL, we will append to the end of that chain. */ 110099a0e58SBosko Milekic if (m != NULL) 111099a0e58SBosko Milekic for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next); 112099a0e58SBosko Milekic else 113099a0e58SBosko Milekic mtail = NULL; 114099a0e58SBosko Milekic 115099a0e58SBosko Milekic /* 116099a0e58SBosko Milekic * Calculate how many mbufs+clusters ("packets") we need and how much 117099a0e58SBosko Milekic * leftover there is after that and allocate the first mbuf+cluster 118099a0e58SBosko Milekic * if required. 119099a0e58SBosko Milekic */ 120099a0e58SBosko Milekic num = len / MCLBYTES; 121099a0e58SBosko Milekic rem = len % MCLBYTES; 122099a0e58SBosko Milekic top = cur = NULL; 123099a0e58SBosko Milekic if (num > 0) { 124099a0e58SBosko Milekic if ((top = cur = m_getcl(how, type, 0)) == NULL) 125099a0e58SBosko Milekic goto failed; 126931f76abSMaxime Henrion top->m_len = 0; 127099a0e58SBosko Milekic } 128099a0e58SBosko Milekic num--; 129099a0e58SBosko Milekic 130099a0e58SBosko Milekic for (i = 0; i < num; i++) { 131099a0e58SBosko Milekic mb = m_getcl(how, type, 0); 132099a0e58SBosko Milekic if (mb == NULL) 133099a0e58SBosko Milekic goto failed; 134099a0e58SBosko Milekic mb->m_len = 0; 135099a0e58SBosko Milekic cur = (cur->m_next = mb); 136099a0e58SBosko Milekic } 137099a0e58SBosko Milekic if (rem > 0) { 138099a0e58SBosko Milekic mb = (rem > MINCLSIZE) ? 139099a0e58SBosko Milekic m_getcl(how, type, 0) : m_get(how, type); 140099a0e58SBosko Milekic if (mb == NULL) 141099a0e58SBosko Milekic goto failed; 142099a0e58SBosko Milekic mb->m_len = 0; 143099a0e58SBosko Milekic if (cur == NULL) 144099a0e58SBosko Milekic top = mb; 145099a0e58SBosko Milekic else 146099a0e58SBosko Milekic cur->m_next = mb; 147099a0e58SBosko Milekic } 148099a0e58SBosko Milekic 149099a0e58SBosko Milekic if (mtail != NULL) 150099a0e58SBosko Milekic mtail->m_next = top; 151099a0e58SBosko Milekic return top; 152099a0e58SBosko Milekic failed: 153099a0e58SBosko Milekic if (top != NULL) 154099a0e58SBosko Milekic m_freem(top); 155099a0e58SBosko Milekic return NULL; 156099a0e58SBosko Milekic } 157099a0e58SBosko Milekic 158099a0e58SBosko Milekic /* 159099a0e58SBosko Milekic * Free an entire chain of mbufs and associated external buffers, if 160099a0e58SBosko Milekic * applicable. 161099a0e58SBosko Milekic */ 162099a0e58SBosko Milekic void 163099a0e58SBosko Milekic m_freem(struct mbuf *mb) 164099a0e58SBosko Milekic { 165099a0e58SBosko Milekic 166099a0e58SBosko Milekic while (mb != NULL) 167099a0e58SBosko Milekic mb = m_free(mb); 168099a0e58SBosko Milekic } 169099a0e58SBosko Milekic 170099a0e58SBosko Milekic /*- 171099a0e58SBosko Milekic * Configure a provided mbuf to refer to the provided external storage 172099a0e58SBosko Milekic * buffer and setup a reference count for said buffer. If the setting 173099a0e58SBosko Milekic * up of the reference count fails, the M_EXT bit will not be set. If 174099a0e58SBosko Milekic * successfull, the M_EXT bit is set in the mbuf's flags. 175099a0e58SBosko Milekic * 176099a0e58SBosko Milekic * Arguments: 177099a0e58SBosko Milekic * mb The existing mbuf to which to attach the provided buffer. 178099a0e58SBosko Milekic * buf The address of the provided external storage buffer. 179099a0e58SBosko Milekic * size The size of the provided buffer. 180099a0e58SBosko Milekic * freef A pointer to a routine that is responsible for freeing the 181099a0e58SBosko Milekic * provided external storage buffer. 182099a0e58SBosko Milekic * args A pointer to an argument structure (of any type) to be passed 183099a0e58SBosko Milekic * to the provided freef routine (may be NULL). 184099a0e58SBosko Milekic * flags Any other flags to be passed to the provided mbuf. 185099a0e58SBosko Milekic * type The type that the external storage buffer should be 186099a0e58SBosko Milekic * labeled with. 187099a0e58SBosko Milekic * 188099a0e58SBosko Milekic * Returns: 189099a0e58SBosko Milekic * Nothing. 190099a0e58SBosko Milekic */ 191099a0e58SBosko Milekic void 192099a0e58SBosko Milekic m_extadd(struct mbuf *mb, caddr_t buf, u_int size, 193099a0e58SBosko Milekic void (*freef)(void *, void *), void *args, int flags, int type) 194099a0e58SBosko Milekic { 195099a0e58SBosko Milekic u_int *ref_cnt = NULL; 196099a0e58SBosko Milekic 197099a0e58SBosko Milekic /* XXX Shouldn't be adding EXT_CLUSTER with this API */ 198099a0e58SBosko Milekic if (type == EXT_CLUSTER) 199099a0e58SBosko Milekic ref_cnt = (u_int *)uma_find_refcnt(zone_clust, 200099a0e58SBosko Milekic mb->m_ext.ext_buf); 201099a0e58SBosko Milekic else if (type == EXT_EXTREF) 202099a0e58SBosko Milekic ref_cnt = mb->m_ext.ref_cnt; 203099a0e58SBosko Milekic mb->m_ext.ref_cnt = (ref_cnt == NULL) ? 204099a0e58SBosko Milekic malloc(sizeof(u_int), M_MBUF, M_NOWAIT) : (u_int *)ref_cnt; 205099a0e58SBosko Milekic if (mb->m_ext.ref_cnt != NULL) { 206099a0e58SBosko Milekic *(mb->m_ext.ref_cnt) = 1; 207099a0e58SBosko Milekic mb->m_flags |= (M_EXT | flags); 208099a0e58SBosko Milekic mb->m_ext.ext_buf = buf; 209099a0e58SBosko Milekic mb->m_data = mb->m_ext.ext_buf; 210099a0e58SBosko Milekic mb->m_ext.ext_size = size; 211099a0e58SBosko Milekic mb->m_ext.ext_free = freef; 212099a0e58SBosko Milekic mb->m_ext.ext_args = args; 213099a0e58SBosko Milekic mb->m_ext.ext_type = type; 214099a0e58SBosko Milekic } 215099a0e58SBosko Milekic } 216099a0e58SBosko Milekic 217099a0e58SBosko Milekic /* 218099a0e58SBosko Milekic * Non-directly-exported function to clean up after mbufs with M_EXT 219099a0e58SBosko Milekic * storage attached to them if the reference count hits 0. 220099a0e58SBosko Milekic */ 221099a0e58SBosko Milekic void 222099a0e58SBosko Milekic mb_free_ext(struct mbuf *m) 223099a0e58SBosko Milekic { 224b5b2ea9aSBosko Milekic u_int cnt; 225099a0e58SBosko Milekic 226b5b2ea9aSBosko Milekic /* 227b5b2ea9aSBosko Milekic * This is tricky. We need to make sure to decrement the 228b5b2ea9aSBosko Milekic * refcount in a safe way but to also clean up if we're the 229b5b2ea9aSBosko Milekic * last reference. This method seems to do it without race. 230b5b2ea9aSBosko Milekic */ 231b5b2ea9aSBosko Milekic do { 232b5b2ea9aSBosko Milekic cnt = *(m->m_ext.ref_cnt); 233b5b2ea9aSBosko Milekic if (atomic_cmpset_int(m->m_ext.ref_cnt, cnt, cnt - 1)) { 234b5b2ea9aSBosko Milekic if (cnt == 1) { 235b5b2ea9aSBosko Milekic /* 236b5b2ea9aSBosko Milekic * Do the free, should be safe. 237b5b2ea9aSBosko Milekic */ 238099a0e58SBosko Milekic if (m->m_ext.ext_type == EXT_PACKET) { 239099a0e58SBosko Milekic uma_zfree(zone_pack, m); 24096e12413SBosko Milekic return; 241099a0e58SBosko Milekic } else if (m->m_ext.ext_type == EXT_CLUSTER) { 242099a0e58SBosko Milekic uma_zfree(zone_clust, m->m_ext.ext_buf); 243099a0e58SBosko Milekic m->m_ext.ext_buf = NULL; 244099a0e58SBosko Milekic } else { 245099a0e58SBosko Milekic (*(m->m_ext.ext_free))(m->m_ext.ext_buf, 246099a0e58SBosko Milekic m->m_ext.ext_args); 247099a0e58SBosko Milekic if (m->m_ext.ext_type != EXT_EXTREF) 248099a0e58SBosko Milekic free(m->m_ext.ref_cnt, M_MBUF); 24996e12413SBosko Milekic m->m_ext.ext_buf = NULL; 250099a0e58SBosko Milekic } 251099a0e58SBosko Milekic } 252b5b2ea9aSBosko Milekic /* Decrement (and potentially free) done, safely. */ 253b5b2ea9aSBosko Milekic break; 254b5b2ea9aSBosko Milekic } 255b5b2ea9aSBosko Milekic } while (1); 25696e12413SBosko Milekic uma_zfree(zone_mbuf, m); 257b5b2ea9aSBosko Milekic } 258099a0e58SBosko Milekic 259099a0e58SBosko Milekic /* 2609967cafcSSam Leffler * "Move" mbuf pkthdr from "from" to "to". 261e37b1fcdSRobert Watson * "from" must have M_PKTHDR set, and "to" must be empty. 262e37b1fcdSRobert Watson */ 263e37b1fcdSRobert Watson void 2649967cafcSSam Leffler m_move_pkthdr(struct mbuf *to, struct mbuf *from) 265e37b1fcdSRobert Watson { 266e37b1fcdSRobert Watson 267e37b1fcdSRobert Watson #if 0 2689967cafcSSam Leffler /* see below for why these are not enabled */ 269fe584538SDag-Erling Smørgrav M_ASSERTPKTHDR(to); 270225bff6fSRobert Watson /* Note: with MAC, this may not be a good assertion. */ 2719967cafcSSam Leffler KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), 2729967cafcSSam Leffler ("m_move_pkthdr: to has tags")); 273e37b1fcdSRobert Watson #endif 2749967cafcSSam Leffler KASSERT((to->m_flags & M_EXT) == 0, ("m_move_pkthdr: to has cluster")); 275e32a5b94SRobert Watson #ifdef MAC 276225bff6fSRobert Watson /* 277225bff6fSRobert Watson * XXXMAC: It could be this should also occur for non-MAC? 278225bff6fSRobert Watson */ 279e32a5b94SRobert Watson if (to->m_flags & M_PKTHDR) 280225bff6fSRobert Watson m_tag_delete_chain(to, NULL); 281e32a5b94SRobert Watson #endif 282e37b1fcdSRobert Watson to->m_flags = from->m_flags & M_COPYFLAGS; 2839967cafcSSam Leffler to->m_data = to->m_pktdat; 2849967cafcSSam Leffler to->m_pkthdr = from->m_pkthdr; /* especially tags */ 2859967cafcSSam Leffler SLIST_INIT(&from->m_pkthdr.tags); /* purge tags from src */ 2869967cafcSSam Leffler from->m_flags &= ~M_PKTHDR; 2879967cafcSSam Leffler } 2889967cafcSSam Leffler 2899967cafcSSam Leffler /* 2909967cafcSSam Leffler * Duplicate "from"'s mbuf pkthdr in "to". 2919967cafcSSam Leffler * "from" must have M_PKTHDR set, and "to" must be empty. 2929967cafcSSam Leffler * In particular, this does a deep copy of the packet tags. 2939967cafcSSam Leffler */ 2949967cafcSSam Leffler int 2959967cafcSSam Leffler m_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how) 2969967cafcSSam Leffler { 2979967cafcSSam Leffler 2989967cafcSSam Leffler #if 0 2999967cafcSSam Leffler /* 3009967cafcSSam Leffler * The mbuf allocator only initializes the pkthdr 3019967cafcSSam Leffler * when the mbuf is allocated with MGETHDR. Many users 3029967cafcSSam Leffler * (e.g. m_copy*, m_prepend) use MGET and then 3039967cafcSSam Leffler * smash the pkthdr as needed causing these 3049967cafcSSam Leffler * assertions to trip. For now just disable them. 3059967cafcSSam Leffler */ 306fe584538SDag-Erling Smørgrav M_ASSERTPKTHDR(to); 307225bff6fSRobert Watson /* Note: with MAC, this may not be a good assertion. */ 3089967cafcSSam Leffler KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), ("m_dup_pkthdr: to has tags")); 3099967cafcSSam Leffler #endif 310063d8114SAlfred Perlstein MBUF_CHECKSLEEP(how); 3119967cafcSSam Leffler #ifdef MAC 3129967cafcSSam Leffler if (to->m_flags & M_PKTHDR) 313225bff6fSRobert Watson m_tag_delete_chain(to, NULL); 3149967cafcSSam Leffler #endif 315df8c7fc9SMike Silbersack to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT); 316df8c7fc9SMike Silbersack if ((to->m_flags & M_EXT) == 0) 3179967cafcSSam Leffler to->m_data = to->m_pktdat; 318e37b1fcdSRobert Watson to->m_pkthdr = from->m_pkthdr; 3199967cafcSSam Leffler SLIST_INIT(&to->m_pkthdr.tags); 320aa65d9f5SRobert Watson return (m_tag_copy_chain(to, from, MBTOM(how))); 321e37b1fcdSRobert Watson } 322e37b1fcdSRobert Watson 323e37b1fcdSRobert Watson /* 324df8bae1dSRodney W. Grimes * Lesser-used path for M_PREPEND: 325df8bae1dSRodney W. Grimes * allocate new mbuf to prepend to chain, 326df8bae1dSRodney W. Grimes * copy junk along. 327df8bae1dSRodney W. Grimes */ 328df8bae1dSRodney W. Grimes struct mbuf * 329122a814aSBosko Milekic m_prepend(struct mbuf *m, int len, int how) 330df8bae1dSRodney W. Grimes { 331df8bae1dSRodney W. Grimes struct mbuf *mn; 332df8bae1dSRodney W. Grimes 333f8bf8e39SMike Silbersack if (m->m_flags & M_PKTHDR) 334f8bf8e39SMike Silbersack MGETHDR(mn, how, m->m_type); 335f8bf8e39SMike Silbersack else 336df8bae1dSRodney W. Grimes MGET(mn, how, m->m_type); 337122a814aSBosko Milekic if (mn == NULL) { 338df8bae1dSRodney W. Grimes m_freem(m); 339122a814aSBosko Milekic return (NULL); 340df8bae1dSRodney W. Grimes } 341225bff6fSRobert Watson if (m->m_flags & M_PKTHDR) 3429967cafcSSam Leffler M_MOVE_PKTHDR(mn, m); 343df8bae1dSRodney W. Grimes mn->m_next = m; 344df8bae1dSRodney W. Grimes m = mn; 345df8bae1dSRodney W. Grimes if (len < MHLEN) 346df8bae1dSRodney W. Grimes MH_ALIGN(m, len); 347df8bae1dSRodney W. Grimes m->m_len = len; 348df8bae1dSRodney W. Grimes return (m); 349df8bae1dSRodney W. Grimes } 350df8bae1dSRodney W. Grimes 351df8bae1dSRodney W. Grimes /* 352df8bae1dSRodney W. Grimes * Make a copy of an mbuf chain starting "off0" bytes from the beginning, 353df8bae1dSRodney W. Grimes * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf. 354a163d034SWarner Losh * The wait parameter is a choice of M_TRYWAIT/M_DONTWAIT from caller. 3551c38f2eaSArchie Cobbs * Note that the copy is read-only, because clusters are not copied, 3561c38f2eaSArchie Cobbs * only their reference counts are incremented. 357df8bae1dSRodney W. Grimes */ 358df8bae1dSRodney W. Grimes struct mbuf * 359122a814aSBosko Milekic m_copym(struct mbuf *m, int off0, int len, int wait) 360df8bae1dSRodney W. Grimes { 361122a814aSBosko Milekic struct mbuf *n, **np; 362122a814aSBosko Milekic int off = off0; 363df8bae1dSRodney W. Grimes struct mbuf *top; 364df8bae1dSRodney W. Grimes int copyhdr = 0; 365df8bae1dSRodney W. Grimes 366e0a653ddSAlfred Perlstein KASSERT(off >= 0, ("m_copym, negative off %d", off)); 367e0a653ddSAlfred Perlstein KASSERT(len >= 0, ("m_copym, negative len %d", len)); 368063d8114SAlfred Perlstein MBUF_CHECKSLEEP(wait); 369df8bae1dSRodney W. Grimes if (off == 0 && m->m_flags & M_PKTHDR) 370df8bae1dSRodney W. Grimes copyhdr = 1; 371df8bae1dSRodney W. Grimes while (off > 0) { 372e0a653ddSAlfred Perlstein KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain")); 373df8bae1dSRodney W. Grimes if (off < m->m_len) 374df8bae1dSRodney W. Grimes break; 375df8bae1dSRodney W. Grimes off -= m->m_len; 376df8bae1dSRodney W. Grimes m = m->m_next; 377df8bae1dSRodney W. Grimes } 378df8bae1dSRodney W. Grimes np = ⊤ 379df8bae1dSRodney W. Grimes top = 0; 380df8bae1dSRodney W. Grimes while (len > 0) { 381122a814aSBosko Milekic if (m == NULL) { 382e0a653ddSAlfred Perlstein KASSERT(len == M_COPYALL, 383e0a653ddSAlfred Perlstein ("m_copym, length > size of mbuf chain")); 384df8bae1dSRodney W. Grimes break; 385df8bae1dSRodney W. Grimes } 386f8bf8e39SMike Silbersack if (copyhdr) 387f8bf8e39SMike Silbersack MGETHDR(n, wait, m->m_type); 388f8bf8e39SMike Silbersack else 389df8bae1dSRodney W. Grimes MGET(n, wait, m->m_type); 390df8bae1dSRodney W. Grimes *np = n; 391122a814aSBosko Milekic if (n == NULL) 392df8bae1dSRodney W. Grimes goto nospace; 393df8bae1dSRodney W. Grimes if (copyhdr) { 3949967cafcSSam Leffler if (!m_dup_pkthdr(n, m, wait)) 3959967cafcSSam Leffler goto nospace; 396df8bae1dSRodney W. Grimes if (len == M_COPYALL) 397df8bae1dSRodney W. Grimes n->m_pkthdr.len -= off0; 398df8bae1dSRodney W. Grimes else 399df8bae1dSRodney W. Grimes n->m_pkthdr.len = len; 400df8bae1dSRodney W. Grimes copyhdr = 0; 401df8bae1dSRodney W. Grimes } 402df8bae1dSRodney W. Grimes n->m_len = min(len, m->m_len - off); 403df8bae1dSRodney W. Grimes if (m->m_flags & M_EXT) { 404df8bae1dSRodney W. Grimes n->m_data = m->m_data + off; 405df8bae1dSRodney W. Grimes n->m_ext = m->m_ext; 406df8bae1dSRodney W. Grimes n->m_flags |= M_EXT; 407a5c4836dSDavid Malone MEXT_ADD_REF(m); 408df8bae1dSRodney W. Grimes } else 409df8bae1dSRodney W. Grimes bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), 410bd395ae8SBosko Milekic (u_int)n->m_len); 411df8bae1dSRodney W. Grimes if (len != M_COPYALL) 412df8bae1dSRodney W. Grimes len -= n->m_len; 413df8bae1dSRodney W. Grimes off = 0; 414df8bae1dSRodney W. Grimes m = m->m_next; 415df8bae1dSRodney W. Grimes np = &n->m_next; 416df8bae1dSRodney W. Grimes } 41708442f8aSBosko Milekic if (top == NULL) 41808442f8aSBosko Milekic mbstat.m_mcfail++; /* XXX: No consistency. */ 41908442f8aSBosko Milekic 420df8bae1dSRodney W. Grimes return (top); 421df8bae1dSRodney W. Grimes nospace: 422df8bae1dSRodney W. Grimes m_freem(top); 42308442f8aSBosko Milekic mbstat.m_mcfail++; /* XXX: No consistency. */ 424122a814aSBosko Milekic return (NULL); 425df8bae1dSRodney W. Grimes } 426df8bae1dSRodney W. Grimes 427df8bae1dSRodney W. Grimes /* 4286a06dea0SGarrett Wollman * Copy an entire packet, including header (which must be present). 4296a06dea0SGarrett Wollman * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'. 4301c38f2eaSArchie Cobbs * Note that the copy is read-only, because clusters are not copied, 4311c38f2eaSArchie Cobbs * only their reference counts are incremented. 4325fe86675SLuigi Rizzo * Preserve alignment of the first mbuf so if the creator has left 4335fe86675SLuigi Rizzo * some room at the beginning (e.g. for inserting protocol headers) 4345fe86675SLuigi Rizzo * the copies still have the room available. 4356a06dea0SGarrett Wollman */ 4366a06dea0SGarrett Wollman struct mbuf * 437122a814aSBosko Milekic m_copypacket(struct mbuf *m, int how) 4386a06dea0SGarrett Wollman { 4396a06dea0SGarrett Wollman struct mbuf *top, *n, *o; 4406a06dea0SGarrett Wollman 441063d8114SAlfred Perlstein MBUF_CHECKSLEEP(how); 4426a06dea0SGarrett Wollman MGET(n, how, m->m_type); 4436a06dea0SGarrett Wollman top = n; 444122a814aSBosko Milekic if (n == NULL) 4456a06dea0SGarrett Wollman goto nospace; 4466a06dea0SGarrett Wollman 4479967cafcSSam Leffler if (!m_dup_pkthdr(n, m, how)) 4489967cafcSSam Leffler goto nospace; 4496a06dea0SGarrett Wollman n->m_len = m->m_len; 4506a06dea0SGarrett Wollman if (m->m_flags & M_EXT) { 4516a06dea0SGarrett Wollman n->m_data = m->m_data; 4526a06dea0SGarrett Wollman n->m_ext = m->m_ext; 4536a06dea0SGarrett Wollman n->m_flags |= M_EXT; 454a5c4836dSDavid Malone MEXT_ADD_REF(m); 4556a06dea0SGarrett Wollman } else { 4565fe86675SLuigi Rizzo n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat ); 4576a06dea0SGarrett Wollman bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 4586a06dea0SGarrett Wollman } 4596a06dea0SGarrett Wollman 4606a06dea0SGarrett Wollman m = m->m_next; 4616a06dea0SGarrett Wollman while (m) { 4626a06dea0SGarrett Wollman MGET(o, how, m->m_type); 463122a814aSBosko Milekic if (o == NULL) 4646a06dea0SGarrett Wollman goto nospace; 4656a06dea0SGarrett Wollman 4666a06dea0SGarrett Wollman n->m_next = o; 4676a06dea0SGarrett Wollman n = n->m_next; 4686a06dea0SGarrett Wollman 4696a06dea0SGarrett Wollman n->m_len = m->m_len; 4706a06dea0SGarrett Wollman if (m->m_flags & M_EXT) { 4716a06dea0SGarrett Wollman n->m_data = m->m_data; 4726a06dea0SGarrett Wollman n->m_ext = m->m_ext; 4736a06dea0SGarrett Wollman n->m_flags |= M_EXT; 474a5c4836dSDavid Malone MEXT_ADD_REF(m); 4756a06dea0SGarrett Wollman } else { 4766a06dea0SGarrett Wollman bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 4776a06dea0SGarrett Wollman } 4786a06dea0SGarrett Wollman 4796a06dea0SGarrett Wollman m = m->m_next; 4806a06dea0SGarrett Wollman } 4816a06dea0SGarrett Wollman return top; 4826a06dea0SGarrett Wollman nospace: 4836a06dea0SGarrett Wollman m_freem(top); 48408442f8aSBosko Milekic mbstat.m_mcfail++; /* XXX: No consistency. */ 485122a814aSBosko Milekic return (NULL); 4866a06dea0SGarrett Wollman } 4876a06dea0SGarrett Wollman 4886a06dea0SGarrett Wollman /* 489df8bae1dSRodney W. Grimes * Copy data from an mbuf chain starting "off" bytes from the beginning, 490df8bae1dSRodney W. Grimes * continuing for "len" bytes, into the indicated buffer. 491df8bae1dSRodney W. Grimes */ 49226f9a767SRodney W. Grimes void 493a8cfc0eeSJulian Elischer m_copydata(const struct mbuf *m, int off, int len, caddr_t cp) 494df8bae1dSRodney W. Grimes { 495bd395ae8SBosko Milekic u_int count; 496df8bae1dSRodney W. Grimes 497e0a653ddSAlfred Perlstein KASSERT(off >= 0, ("m_copydata, negative off %d", off)); 498e0a653ddSAlfred Perlstein KASSERT(len >= 0, ("m_copydata, negative len %d", len)); 499df8bae1dSRodney W. Grimes while (off > 0) { 500e0a653ddSAlfred Perlstein KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain")); 501df8bae1dSRodney W. Grimes if (off < m->m_len) 502df8bae1dSRodney W. Grimes break; 503df8bae1dSRodney W. Grimes off -= m->m_len; 504df8bae1dSRodney W. Grimes m = m->m_next; 505df8bae1dSRodney W. Grimes } 506df8bae1dSRodney W. Grimes while (len > 0) { 507e0a653ddSAlfred Perlstein KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain")); 508df8bae1dSRodney W. Grimes count = min(m->m_len - off, len); 509df8bae1dSRodney W. Grimes bcopy(mtod(m, caddr_t) + off, cp, count); 510df8bae1dSRodney W. Grimes len -= count; 511df8bae1dSRodney W. Grimes cp += count; 512df8bae1dSRodney W. Grimes off = 0; 513df8bae1dSRodney W. Grimes m = m->m_next; 514df8bae1dSRodney W. Grimes } 515df8bae1dSRodney W. Grimes } 516df8bae1dSRodney W. Grimes 517df8bae1dSRodney W. Grimes /* 5181c38f2eaSArchie Cobbs * Copy a packet header mbuf chain into a completely new chain, including 5191c38f2eaSArchie Cobbs * copying any mbuf clusters. Use this instead of m_copypacket() when 5201c38f2eaSArchie Cobbs * you need a writable copy of an mbuf chain. 5211c38f2eaSArchie Cobbs */ 5221c38f2eaSArchie Cobbs struct mbuf * 523122a814aSBosko Milekic m_dup(struct mbuf *m, int how) 5241c38f2eaSArchie Cobbs { 5251c38f2eaSArchie Cobbs struct mbuf **p, *top = NULL; 5261c38f2eaSArchie Cobbs int remain, moff, nsize; 5271c38f2eaSArchie Cobbs 528063d8114SAlfred Perlstein MBUF_CHECKSLEEP(how); 5291c38f2eaSArchie Cobbs /* Sanity check */ 5301c38f2eaSArchie Cobbs if (m == NULL) 531122a814aSBosko Milekic return (NULL); 532fe584538SDag-Erling Smørgrav M_ASSERTPKTHDR(m); 5331c38f2eaSArchie Cobbs 5341c38f2eaSArchie Cobbs /* While there's more data, get a new mbuf, tack it on, and fill it */ 5351c38f2eaSArchie Cobbs remain = m->m_pkthdr.len; 5361c38f2eaSArchie Cobbs moff = 0; 5371c38f2eaSArchie Cobbs p = ⊤ 5381c38f2eaSArchie Cobbs while (remain > 0 || top == NULL) { /* allow m->m_pkthdr.len == 0 */ 5391c38f2eaSArchie Cobbs struct mbuf *n; 5401c38f2eaSArchie Cobbs 5411c38f2eaSArchie Cobbs /* Get the next new mbuf */ 542099a0e58SBosko Milekic if (remain >= MINCLSIZE) { 543099a0e58SBosko Milekic n = m_getcl(how, m->m_type, 0); 544099a0e58SBosko Milekic nsize = MCLBYTES; 545099a0e58SBosko Milekic } else { 546099a0e58SBosko Milekic n = m_get(how, m->m_type); 547099a0e58SBosko Milekic nsize = MLEN; 548099a0e58SBosko Milekic } 5491c38f2eaSArchie Cobbs if (n == NULL) 5501c38f2eaSArchie Cobbs goto nospace; 551099a0e58SBosko Milekic 552099a0e58SBosko Milekic if (top == NULL) { /* First one, must be PKTHDR */ 553099a0e58SBosko Milekic if (!m_dup_pkthdr(n, m, how)) { 554099a0e58SBosko Milekic m_free(n); 5551c38f2eaSArchie Cobbs goto nospace; 5561c38f2eaSArchie Cobbs } 557099a0e58SBosko Milekic nsize = MHLEN; 5581c38f2eaSArchie Cobbs } 5591c38f2eaSArchie Cobbs n->m_len = 0; 5601c38f2eaSArchie Cobbs 5611c38f2eaSArchie Cobbs /* Link it into the new chain */ 5621c38f2eaSArchie Cobbs *p = n; 5631c38f2eaSArchie Cobbs p = &n->m_next; 5641c38f2eaSArchie Cobbs 5651c38f2eaSArchie Cobbs /* Copy data from original mbuf(s) into new mbuf */ 5661c38f2eaSArchie Cobbs while (n->m_len < nsize && m != NULL) { 5671c38f2eaSArchie Cobbs int chunk = min(nsize - n->m_len, m->m_len - moff); 5681c38f2eaSArchie Cobbs 5691c38f2eaSArchie Cobbs bcopy(m->m_data + moff, n->m_data + n->m_len, chunk); 5701c38f2eaSArchie Cobbs moff += chunk; 5711c38f2eaSArchie Cobbs n->m_len += chunk; 5721c38f2eaSArchie Cobbs remain -= chunk; 5731c38f2eaSArchie Cobbs if (moff == m->m_len) { 5741c38f2eaSArchie Cobbs m = m->m_next; 5751c38f2eaSArchie Cobbs moff = 0; 5761c38f2eaSArchie Cobbs } 5771c38f2eaSArchie Cobbs } 5781c38f2eaSArchie Cobbs 5791c38f2eaSArchie Cobbs /* Check correct total mbuf length */ 5801c38f2eaSArchie Cobbs KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL), 581a48740b6SDavid E. O'Brien ("%s: bogus m_pkthdr.len", __func__)); 5821c38f2eaSArchie Cobbs } 5831c38f2eaSArchie Cobbs return (top); 5841c38f2eaSArchie Cobbs 5851c38f2eaSArchie Cobbs nospace: 5861c38f2eaSArchie Cobbs m_freem(top); 58708442f8aSBosko Milekic mbstat.m_mcfail++; /* XXX: No consistency. */ 588122a814aSBosko Milekic return (NULL); 5891c38f2eaSArchie Cobbs } 5901c38f2eaSArchie Cobbs 5911c38f2eaSArchie Cobbs /* 592df8bae1dSRodney W. Grimes * Concatenate mbuf chain n to m. 593df8bae1dSRodney W. Grimes * Both chains must be of the same type (e.g. MT_DATA). 594df8bae1dSRodney W. Grimes * Any m_pkthdr is not updated. 595df8bae1dSRodney W. Grimes */ 59626f9a767SRodney W. Grimes void 597122a814aSBosko Milekic m_cat(struct mbuf *m, struct mbuf *n) 598df8bae1dSRodney W. Grimes { 599df8bae1dSRodney W. Grimes while (m->m_next) 600df8bae1dSRodney W. Grimes m = m->m_next; 601df8bae1dSRodney W. Grimes while (n) { 602df8bae1dSRodney W. Grimes if (m->m_flags & M_EXT || 603df8bae1dSRodney W. Grimes m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) { 604df8bae1dSRodney W. Grimes /* just join the two chains */ 605df8bae1dSRodney W. Grimes m->m_next = n; 606df8bae1dSRodney W. Grimes return; 607df8bae1dSRodney W. Grimes } 608df8bae1dSRodney W. Grimes /* splat the data from one into the other */ 609df8bae1dSRodney W. Grimes bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 610df8bae1dSRodney W. Grimes (u_int)n->m_len); 611df8bae1dSRodney W. Grimes m->m_len += n->m_len; 612df8bae1dSRodney W. Grimes n = m_free(n); 613df8bae1dSRodney W. Grimes } 614df8bae1dSRodney W. Grimes } 615df8bae1dSRodney W. Grimes 61626f9a767SRodney W. Grimes void 617122a814aSBosko Milekic m_adj(struct mbuf *mp, int req_len) 618df8bae1dSRodney W. Grimes { 619122a814aSBosko Milekic int len = req_len; 620122a814aSBosko Milekic struct mbuf *m; 621122a814aSBosko Milekic int count; 622df8bae1dSRodney W. Grimes 623df8bae1dSRodney W. Grimes if ((m = mp) == NULL) 624df8bae1dSRodney W. Grimes return; 625df8bae1dSRodney W. Grimes if (len >= 0) { 626df8bae1dSRodney W. Grimes /* 627df8bae1dSRodney W. Grimes * Trim from head. 628df8bae1dSRodney W. Grimes */ 629df8bae1dSRodney W. Grimes while (m != NULL && len > 0) { 630df8bae1dSRodney W. Grimes if (m->m_len <= len) { 631df8bae1dSRodney W. Grimes len -= m->m_len; 632df8bae1dSRodney W. Grimes m->m_len = 0; 633df8bae1dSRodney W. Grimes m = m->m_next; 634df8bae1dSRodney W. Grimes } else { 635df8bae1dSRodney W. Grimes m->m_len -= len; 636df8bae1dSRodney W. Grimes m->m_data += len; 637df8bae1dSRodney W. Grimes len = 0; 638df8bae1dSRodney W. Grimes } 639df8bae1dSRodney W. Grimes } 640df8bae1dSRodney W. Grimes m = mp; 641df8bae1dSRodney W. Grimes if (mp->m_flags & M_PKTHDR) 642df8bae1dSRodney W. Grimes m->m_pkthdr.len -= (req_len - len); 643df8bae1dSRodney W. Grimes } else { 644df8bae1dSRodney W. Grimes /* 645df8bae1dSRodney W. Grimes * Trim from tail. Scan the mbuf chain, 646df8bae1dSRodney W. Grimes * calculating its length and finding the last mbuf. 647df8bae1dSRodney W. Grimes * If the adjustment only affects this mbuf, then just 648df8bae1dSRodney W. Grimes * adjust and return. Otherwise, rescan and truncate 649df8bae1dSRodney W. Grimes * after the remaining size. 650df8bae1dSRodney W. Grimes */ 651df8bae1dSRodney W. Grimes len = -len; 652df8bae1dSRodney W. Grimes count = 0; 653df8bae1dSRodney W. Grimes for (;;) { 654df8bae1dSRodney W. Grimes count += m->m_len; 655df8bae1dSRodney W. Grimes if (m->m_next == (struct mbuf *)0) 656df8bae1dSRodney W. Grimes break; 657df8bae1dSRodney W. Grimes m = m->m_next; 658df8bae1dSRodney W. Grimes } 659df8bae1dSRodney W. Grimes if (m->m_len >= len) { 660df8bae1dSRodney W. Grimes m->m_len -= len; 661df8bae1dSRodney W. Grimes if (mp->m_flags & M_PKTHDR) 662df8bae1dSRodney W. Grimes mp->m_pkthdr.len -= len; 663df8bae1dSRodney W. Grimes return; 664df8bae1dSRodney W. Grimes } 665df8bae1dSRodney W. Grimes count -= len; 666df8bae1dSRodney W. Grimes if (count < 0) 667df8bae1dSRodney W. Grimes count = 0; 668df8bae1dSRodney W. Grimes /* 669df8bae1dSRodney W. Grimes * Correct length for chain is "count". 670df8bae1dSRodney W. Grimes * Find the mbuf with last data, adjust its length, 671df8bae1dSRodney W. Grimes * and toss data from remaining mbufs on chain. 672df8bae1dSRodney W. Grimes */ 673df8bae1dSRodney W. Grimes m = mp; 674df8bae1dSRodney W. Grimes if (m->m_flags & M_PKTHDR) 675df8bae1dSRodney W. Grimes m->m_pkthdr.len = count; 676df8bae1dSRodney W. Grimes for (; m; m = m->m_next) { 677df8bae1dSRodney W. Grimes if (m->m_len >= count) { 678df8bae1dSRodney W. Grimes m->m_len = count; 679df8bae1dSRodney W. Grimes break; 680df8bae1dSRodney W. Grimes } 681df8bae1dSRodney W. Grimes count -= m->m_len; 682df8bae1dSRodney W. Grimes } 683797f2d22SPoul-Henning Kamp while (m->m_next) 684797f2d22SPoul-Henning Kamp (m = m->m_next) ->m_len = 0; 685df8bae1dSRodney W. Grimes } 686df8bae1dSRodney W. Grimes } 687df8bae1dSRodney W. Grimes 688df8bae1dSRodney W. Grimes /* 689df8bae1dSRodney W. Grimes * Rearange an mbuf chain so that len bytes are contiguous 690df8bae1dSRodney W. Grimes * and in the data area of an mbuf (so that mtod and dtom 691df8bae1dSRodney W. Grimes * will work for a structure of size len). Returns the resulting 692df8bae1dSRodney W. Grimes * mbuf chain on success, frees it and returns null on failure. 693df8bae1dSRodney W. Grimes * If there is room, it will add up to max_protohdr-len extra bytes to the 694df8bae1dSRodney W. Grimes * contiguous region in an attempt to avoid being called next time. 695df8bae1dSRodney W. Grimes */ 696df8bae1dSRodney W. Grimes struct mbuf * 697122a814aSBosko Milekic m_pullup(struct mbuf *n, int len) 698df8bae1dSRodney W. Grimes { 699122a814aSBosko Milekic struct mbuf *m; 700122a814aSBosko Milekic int count; 701df8bae1dSRodney W. Grimes int space; 702df8bae1dSRodney W. Grimes 703df8bae1dSRodney W. Grimes /* 704df8bae1dSRodney W. Grimes * If first mbuf has no cluster, and has room for len bytes 705df8bae1dSRodney W. Grimes * without shifting current data, pullup into it, 706df8bae1dSRodney W. Grimes * otherwise allocate a new mbuf to prepend to the chain. 707df8bae1dSRodney W. Grimes */ 708df8bae1dSRodney W. Grimes if ((n->m_flags & M_EXT) == 0 && 709df8bae1dSRodney W. Grimes n->m_data + len < &n->m_dat[MLEN] && n->m_next) { 710df8bae1dSRodney W. Grimes if (n->m_len >= len) 711df8bae1dSRodney W. Grimes return (n); 712df8bae1dSRodney W. Grimes m = n; 713df8bae1dSRodney W. Grimes n = n->m_next; 714df8bae1dSRodney W. Grimes len -= m->m_len; 715df8bae1dSRodney W. Grimes } else { 716df8bae1dSRodney W. Grimes if (len > MHLEN) 717df8bae1dSRodney W. Grimes goto bad; 718a163d034SWarner Losh MGET(m, M_DONTWAIT, n->m_type); 719122a814aSBosko Milekic if (m == NULL) 720df8bae1dSRodney W. Grimes goto bad; 721df8bae1dSRodney W. Grimes m->m_len = 0; 7229967cafcSSam Leffler if (n->m_flags & M_PKTHDR) 7239967cafcSSam Leffler M_MOVE_PKTHDR(m, n); 724df8bae1dSRodney W. Grimes } 725df8bae1dSRodney W. Grimes space = &m->m_dat[MLEN] - (m->m_data + m->m_len); 726df8bae1dSRodney W. Grimes do { 727df8bae1dSRodney W. Grimes count = min(min(max(len, max_protohdr), space), n->m_len); 728df8bae1dSRodney W. Grimes bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 729bd395ae8SBosko Milekic (u_int)count); 730df8bae1dSRodney W. Grimes len -= count; 731df8bae1dSRodney W. Grimes m->m_len += count; 732df8bae1dSRodney W. Grimes n->m_len -= count; 733df8bae1dSRodney W. Grimes space -= count; 734df8bae1dSRodney W. Grimes if (n->m_len) 735df8bae1dSRodney W. Grimes n->m_data += count; 736df8bae1dSRodney W. Grimes else 737df8bae1dSRodney W. Grimes n = m_free(n); 738df8bae1dSRodney W. Grimes } while (len > 0 && n); 739df8bae1dSRodney W. Grimes if (len > 0) { 740df8bae1dSRodney W. Grimes (void) m_free(m); 741df8bae1dSRodney W. Grimes goto bad; 742df8bae1dSRodney W. Grimes } 743df8bae1dSRodney W. Grimes m->m_next = n; 744df8bae1dSRodney W. Grimes return (m); 745df8bae1dSRodney W. Grimes bad: 746df8bae1dSRodney W. Grimes m_freem(n); 74708442f8aSBosko Milekic mbstat.m_mpfail++; /* XXX: No consistency. */ 748122a814aSBosko Milekic return (NULL); 749df8bae1dSRodney W. Grimes } 750df8bae1dSRodney W. Grimes 751df8bae1dSRodney W. Grimes /* 752df8bae1dSRodney W. Grimes * Partition an mbuf chain in two pieces, returning the tail -- 753df8bae1dSRodney W. Grimes * all but the first len0 bytes. In case of failure, it returns NULL and 754df8bae1dSRodney W. Grimes * attempts to restore the chain to its original state. 75548d183faSArchie Cobbs * 75648d183faSArchie Cobbs * Note that the resulting mbufs might be read-only, because the new 75748d183faSArchie Cobbs * mbuf can end up sharing an mbuf cluster with the original mbuf if 75848d183faSArchie Cobbs * the "breaking point" happens to lie within a cluster mbuf. Use the 75948d183faSArchie Cobbs * M_WRITABLE() macro to check for this case. 760df8bae1dSRodney W. Grimes */ 761df8bae1dSRodney W. Grimes struct mbuf * 762122a814aSBosko Milekic m_split(struct mbuf *m0, int len0, int wait) 763df8bae1dSRodney W. Grimes { 764122a814aSBosko Milekic struct mbuf *m, *n; 765bd395ae8SBosko Milekic u_int len = len0, remain; 766df8bae1dSRodney W. Grimes 767063d8114SAlfred Perlstein MBUF_CHECKSLEEP(wait); 768df8bae1dSRodney W. Grimes for (m = m0; m && len > m->m_len; m = m->m_next) 769df8bae1dSRodney W. Grimes len -= m->m_len; 770122a814aSBosko Milekic if (m == NULL) 771122a814aSBosko Milekic return (NULL); 772df8bae1dSRodney W. Grimes remain = m->m_len - len; 773df8bae1dSRodney W. Grimes if (m0->m_flags & M_PKTHDR) { 774df8bae1dSRodney W. Grimes MGETHDR(n, wait, m0->m_type); 775122a814aSBosko Milekic if (n == NULL) 776122a814aSBosko Milekic return (NULL); 777df8bae1dSRodney W. Grimes n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif; 778df8bae1dSRodney W. Grimes n->m_pkthdr.len = m0->m_pkthdr.len - len0; 779df8bae1dSRodney W. Grimes m0->m_pkthdr.len = len0; 780df8bae1dSRodney W. Grimes if (m->m_flags & M_EXT) 781df8bae1dSRodney W. Grimes goto extpacket; 782df8bae1dSRodney W. Grimes if (remain > MHLEN) { 783df8bae1dSRodney W. Grimes /* m can't be the lead packet */ 784df8bae1dSRodney W. Grimes MH_ALIGN(n, 0); 785df8bae1dSRodney W. Grimes n->m_next = m_split(m, len, wait); 786122a814aSBosko Milekic if (n->m_next == NULL) { 787df8bae1dSRodney W. Grimes (void) m_free(n); 788122a814aSBosko Milekic return (NULL); 78940376987SJeffrey Hsu } else { 79040376987SJeffrey Hsu n->m_len = 0; 791df8bae1dSRodney W. Grimes return (n); 79240376987SJeffrey Hsu } 793df8bae1dSRodney W. Grimes } else 794df8bae1dSRodney W. Grimes MH_ALIGN(n, remain); 795df8bae1dSRodney W. Grimes } else if (remain == 0) { 796df8bae1dSRodney W. Grimes n = m->m_next; 797122a814aSBosko Milekic m->m_next = NULL; 798df8bae1dSRodney W. Grimes return (n); 799df8bae1dSRodney W. Grimes } else { 800df8bae1dSRodney W. Grimes MGET(n, wait, m->m_type); 801122a814aSBosko Milekic if (n == NULL) 802122a814aSBosko Milekic return (NULL); 803df8bae1dSRodney W. Grimes M_ALIGN(n, remain); 804df8bae1dSRodney W. Grimes } 805df8bae1dSRodney W. Grimes extpacket: 806df8bae1dSRodney W. Grimes if (m->m_flags & M_EXT) { 807df8bae1dSRodney W. Grimes n->m_flags |= M_EXT; 808df8bae1dSRodney W. Grimes n->m_ext = m->m_ext; 809a5c4836dSDavid Malone MEXT_ADD_REF(m); 810df8bae1dSRodney W. Grimes n->m_data = m->m_data + len; 811df8bae1dSRodney W. Grimes } else { 812df8bae1dSRodney W. Grimes bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain); 813df8bae1dSRodney W. Grimes } 814df8bae1dSRodney W. Grimes n->m_len = remain; 815df8bae1dSRodney W. Grimes m->m_len = len; 816df8bae1dSRodney W. Grimes n->m_next = m->m_next; 817122a814aSBosko Milekic m->m_next = NULL; 818df8bae1dSRodney W. Grimes return (n); 819df8bae1dSRodney W. Grimes } 820df8bae1dSRodney W. Grimes /* 821df8bae1dSRodney W. Grimes * Routine to copy from device local memory into mbufs. 822f5eece3fSBosko Milekic * Note that `off' argument is offset into first mbuf of target chain from 823f5eece3fSBosko Milekic * which to begin copying the data to. 824df8bae1dSRodney W. Grimes */ 825df8bae1dSRodney W. Grimes struct mbuf * 826f5eece3fSBosko Milekic m_devget(char *buf, int totlen, int off, struct ifnet *ifp, 827122a814aSBosko Milekic void (*copy)(char *from, caddr_t to, u_int len)) 828df8bae1dSRodney W. Grimes { 829122a814aSBosko Milekic struct mbuf *m; 830099a0e58SBosko Milekic struct mbuf *top = NULL, **mp = ⊤ 831f5eece3fSBosko Milekic int len; 832df8bae1dSRodney W. Grimes 833f5eece3fSBosko Milekic if (off < 0 || off > MHLEN) 834f5eece3fSBosko Milekic return (NULL); 835f5eece3fSBosko Milekic 836df8bae1dSRodney W. Grimes while (totlen > 0) { 837099a0e58SBosko Milekic if (top == NULL) { /* First one, must be PKTHDR */ 838f5eece3fSBosko Milekic if (totlen + off >= MINCLSIZE) { 839099a0e58SBosko Milekic m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 840f5eece3fSBosko Milekic len = MCLBYTES; 841df8bae1dSRodney W. Grimes } else { 842099a0e58SBosko Milekic m = m_gethdr(M_DONTWAIT, MT_DATA); 843099a0e58SBosko Milekic len = MHLEN; 844099a0e58SBosko Milekic 845099a0e58SBosko Milekic /* Place initial small packet/header at end of mbuf */ 846099a0e58SBosko Milekic if (m && totlen + off + max_linkhdr <= MLEN) { 847df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 848f5eece3fSBosko Milekic len -= max_linkhdr; 849df8bae1dSRodney W. Grimes } 850f5eece3fSBosko Milekic } 851099a0e58SBosko Milekic if (m == NULL) 852099a0e58SBosko Milekic return NULL; 853099a0e58SBosko Milekic m->m_pkthdr.rcvif = ifp; 854099a0e58SBosko Milekic m->m_pkthdr.len = totlen; 855099a0e58SBosko Milekic } else { 856099a0e58SBosko Milekic if (totlen + off >= MINCLSIZE) { 857099a0e58SBosko Milekic m = m_getcl(M_DONTWAIT, MT_DATA, 0); 858099a0e58SBosko Milekic len = MCLBYTES; 859099a0e58SBosko Milekic } else { 860099a0e58SBosko Milekic m = m_get(M_DONTWAIT, MT_DATA); 861099a0e58SBosko Milekic len = MLEN; 862099a0e58SBosko Milekic } 863099a0e58SBosko Milekic if (m == NULL) { 864099a0e58SBosko Milekic m_freem(top); 865099a0e58SBosko Milekic return NULL; 866099a0e58SBosko Milekic } 867099a0e58SBosko Milekic } 868f5eece3fSBosko Milekic if (off) { 869f5eece3fSBosko Milekic m->m_data += off; 870f5eece3fSBosko Milekic len -= off; 871f5eece3fSBosko Milekic off = 0; 872f5eece3fSBosko Milekic } 873f5eece3fSBosko Milekic m->m_len = len = min(totlen, len); 874df8bae1dSRodney W. Grimes if (copy) 875bd395ae8SBosko Milekic copy(buf, mtod(m, caddr_t), (u_int)len); 876df8bae1dSRodney W. Grimes else 877bd395ae8SBosko Milekic bcopy(buf, mtod(m, caddr_t), (u_int)len); 878f5eece3fSBosko Milekic buf += len; 879df8bae1dSRodney W. Grimes *mp = m; 880df8bae1dSRodney W. Grimes mp = &m->m_next; 881df8bae1dSRodney W. Grimes totlen -= len; 882df8bae1dSRodney W. Grimes } 883df8bae1dSRodney W. Grimes return (top); 884df8bae1dSRodney W. Grimes } 885c5789ba3SPoul-Henning Kamp 886c5789ba3SPoul-Henning Kamp /* 887c5789ba3SPoul-Henning Kamp * Copy data from a buffer back into the indicated mbuf chain, 888c5789ba3SPoul-Henning Kamp * starting "off" bytes from the beginning, extending the mbuf 889c5789ba3SPoul-Henning Kamp * chain if necessary. 890c5789ba3SPoul-Henning Kamp */ 891c5789ba3SPoul-Henning Kamp void 89224665342SLuigi Rizzo m_copyback(struct mbuf *m0, int off, int len, c_caddr_t cp) 893c5789ba3SPoul-Henning Kamp { 894122a814aSBosko Milekic int mlen; 895122a814aSBosko Milekic struct mbuf *m = m0, *n; 896c5789ba3SPoul-Henning Kamp int totlen = 0; 897c5789ba3SPoul-Henning Kamp 898122a814aSBosko Milekic if (m0 == NULL) 899c5789ba3SPoul-Henning Kamp return; 900c5789ba3SPoul-Henning Kamp while (off > (mlen = m->m_len)) { 901c5789ba3SPoul-Henning Kamp off -= mlen; 902c5789ba3SPoul-Henning Kamp totlen += mlen; 903122a814aSBosko Milekic if (m->m_next == NULL) { 904099a0e58SBosko Milekic n = m_get(M_DONTWAIT, m->m_type); 905122a814aSBosko Milekic if (n == NULL) 906c5789ba3SPoul-Henning Kamp goto out; 907099a0e58SBosko Milekic bzero(mtod(n, caddr_t), MLEN); 908c5789ba3SPoul-Henning Kamp n->m_len = min(MLEN, len + off); 909c5789ba3SPoul-Henning Kamp m->m_next = n; 910c5789ba3SPoul-Henning Kamp } 911c5789ba3SPoul-Henning Kamp m = m->m_next; 912c5789ba3SPoul-Henning Kamp } 913c5789ba3SPoul-Henning Kamp while (len > 0) { 914c5789ba3SPoul-Henning Kamp mlen = min (m->m_len - off, len); 915bd395ae8SBosko Milekic bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen); 916c5789ba3SPoul-Henning Kamp cp += mlen; 917c5789ba3SPoul-Henning Kamp len -= mlen; 918c5789ba3SPoul-Henning Kamp mlen += off; 919c5789ba3SPoul-Henning Kamp off = 0; 920c5789ba3SPoul-Henning Kamp totlen += mlen; 921c5789ba3SPoul-Henning Kamp if (len == 0) 922c5789ba3SPoul-Henning Kamp break; 923122a814aSBosko Milekic if (m->m_next == NULL) { 924a163d034SWarner Losh n = m_get(M_DONTWAIT, m->m_type); 925122a814aSBosko Milekic if (n == NULL) 926c5789ba3SPoul-Henning Kamp break; 927c5789ba3SPoul-Henning Kamp n->m_len = min(MLEN, len); 928c5789ba3SPoul-Henning Kamp m->m_next = n; 929c5789ba3SPoul-Henning Kamp } 930c5789ba3SPoul-Henning Kamp m = m->m_next; 931c5789ba3SPoul-Henning Kamp } 932c5789ba3SPoul-Henning Kamp out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) 933c5789ba3SPoul-Henning Kamp m->m_pkthdr.len = totlen; 934c5789ba3SPoul-Henning Kamp } 935ce4a64f7SPoul-Henning Kamp 93637621fd5SBruce M Simpson /* 9374873d175SSam Leffler * Append the specified data to the indicated mbuf chain, 9384873d175SSam Leffler * Extend the mbuf chain if the new data does not fit in 9394873d175SSam Leffler * existing space. 9404873d175SSam Leffler * 9414873d175SSam Leffler * Return 1 if able to complete the job; otherwise 0. 9424873d175SSam Leffler */ 9434873d175SSam Leffler int 9444873d175SSam Leffler m_append(struct mbuf *m0, int len, c_caddr_t cp) 9454873d175SSam Leffler { 9464873d175SSam Leffler struct mbuf *m, *n; 9474873d175SSam Leffler int remainder, space; 9484873d175SSam Leffler 9494873d175SSam Leffler for (m = m0; m->m_next != NULL; m = m->m_next) 9504873d175SSam Leffler ; 9514873d175SSam Leffler remainder = len; 9524873d175SSam Leffler space = M_TRAILINGSPACE(m); 9534873d175SSam Leffler if (space > 0) { 9544873d175SSam Leffler /* 9554873d175SSam Leffler * Copy into available space. 9564873d175SSam Leffler */ 9574873d175SSam Leffler if (space > remainder) 9584873d175SSam Leffler space = remainder; 9594873d175SSam Leffler bcopy(cp, mtod(m, caddr_t) + m->m_len, space); 9604873d175SSam Leffler m->m_len += space; 9614873d175SSam Leffler cp += space, remainder -= space; 9624873d175SSam Leffler } 9634873d175SSam Leffler while (remainder > 0) { 9644873d175SSam Leffler /* 9654873d175SSam Leffler * Allocate a new mbuf; could check space 9664873d175SSam Leffler * and allocate a cluster instead. 9674873d175SSam Leffler */ 9684873d175SSam Leffler n = m_get(M_DONTWAIT, m->m_type); 9694873d175SSam Leffler if (n == NULL) 9704873d175SSam Leffler break; 9714873d175SSam Leffler n->m_len = min(MLEN, remainder); 972a37c415eSSam Leffler bcopy(cp, mtod(n, caddr_t), n->m_len); 973a37c415eSSam Leffler cp += n->m_len, remainder -= n->m_len; 9744873d175SSam Leffler m->m_next = n; 9754873d175SSam Leffler m = n; 9764873d175SSam Leffler } 9774873d175SSam Leffler if (m0->m_flags & M_PKTHDR) 9784873d175SSam Leffler m0->m_pkthdr.len += len - remainder; 9794873d175SSam Leffler return (remainder == 0); 9804873d175SSam Leffler } 9814873d175SSam Leffler 9824873d175SSam Leffler /* 98337621fd5SBruce M Simpson * Apply function f to the data in an mbuf chain starting "off" bytes from 98437621fd5SBruce M Simpson * the beginning, continuing for "len" bytes. 98537621fd5SBruce M Simpson */ 98637621fd5SBruce M Simpson int 98737621fd5SBruce M Simpson m_apply(struct mbuf *m, int off, int len, 98854065297SBruce M Simpson int (*f)(void *, void *, u_int), void *arg) 98937621fd5SBruce M Simpson { 99054065297SBruce M Simpson u_int count; 99137621fd5SBruce M Simpson int rval; 99237621fd5SBruce M Simpson 99337621fd5SBruce M Simpson KASSERT(off >= 0, ("m_apply, negative off %d", off)); 99437621fd5SBruce M Simpson KASSERT(len >= 0, ("m_apply, negative len %d", len)); 99537621fd5SBruce M Simpson while (off > 0) { 99637621fd5SBruce M Simpson KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain")); 99737621fd5SBruce M Simpson if (off < m->m_len) 99837621fd5SBruce M Simpson break; 99937621fd5SBruce M Simpson off -= m->m_len; 100037621fd5SBruce M Simpson m = m->m_next; 100137621fd5SBruce M Simpson } 100237621fd5SBruce M Simpson while (len > 0) { 100337621fd5SBruce M Simpson KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain")); 100437621fd5SBruce M Simpson count = min(m->m_len - off, len); 100537621fd5SBruce M Simpson rval = (*f)(arg, mtod(m, caddr_t) + off, count); 100637621fd5SBruce M Simpson if (rval) 100737621fd5SBruce M Simpson return (rval); 100837621fd5SBruce M Simpson len -= count; 100937621fd5SBruce M Simpson off = 0; 101037621fd5SBruce M Simpson m = m->m_next; 101137621fd5SBruce M Simpson } 101237621fd5SBruce M Simpson return (0); 101337621fd5SBruce M Simpson } 101437621fd5SBruce M Simpson 101537621fd5SBruce M Simpson /* 101637621fd5SBruce M Simpson * Return a pointer to mbuf/offset of location in mbuf chain. 101737621fd5SBruce M Simpson */ 101837621fd5SBruce M Simpson struct mbuf * 101937621fd5SBruce M Simpson m_getptr(struct mbuf *m, int loc, int *off) 102037621fd5SBruce M Simpson { 102137621fd5SBruce M Simpson 102237621fd5SBruce M Simpson while (loc >= 0) { 102354065297SBruce M Simpson /* Normal end of search. */ 102437621fd5SBruce M Simpson if (m->m_len > loc) { 102537621fd5SBruce M Simpson *off = loc; 102637621fd5SBruce M Simpson return (m); 102737621fd5SBruce M Simpson } else { 102837621fd5SBruce M Simpson loc -= m->m_len; 102937621fd5SBruce M Simpson if (m->m_next == NULL) { 103037621fd5SBruce M Simpson if (loc == 0) { 103154065297SBruce M Simpson /* Point at the end of valid data. */ 103237621fd5SBruce M Simpson *off = m->m_len; 103337621fd5SBruce M Simpson return (m); 103454065297SBruce M Simpson } 103537621fd5SBruce M Simpson return (NULL); 103654065297SBruce M Simpson } 103737621fd5SBruce M Simpson m = m->m_next; 103837621fd5SBruce M Simpson } 103937621fd5SBruce M Simpson } 104037621fd5SBruce M Simpson return (NULL); 104137621fd5SBruce M Simpson } 104237621fd5SBruce M Simpson 1043ce4a64f7SPoul-Henning Kamp void 10447b125090SJohn-Mark Gurney m_print(const struct mbuf *m, int maxlen) 1045ce4a64f7SPoul-Henning Kamp { 1046ce4a64f7SPoul-Henning Kamp int len; 10477b125090SJohn-Mark Gurney int pdata; 10486357e7b5SEivind Eklund const struct mbuf *m2; 1049ce4a64f7SPoul-Henning Kamp 10507b125090SJohn-Mark Gurney if (m->m_flags & M_PKTHDR) 1051ce4a64f7SPoul-Henning Kamp len = m->m_pkthdr.len; 10527b125090SJohn-Mark Gurney else 10537b125090SJohn-Mark Gurney len = -1; 1054ce4a64f7SPoul-Henning Kamp m2 = m; 10557b125090SJohn-Mark Gurney while (m2 != NULL && (len == -1 || len)) { 10567b125090SJohn-Mark Gurney pdata = m2->m_len; 10577b125090SJohn-Mark Gurney if (maxlen != -1 && pdata > maxlen) 10587b125090SJohn-Mark Gurney pdata = maxlen; 10597b125090SJohn-Mark Gurney printf("mbuf: %p len: %d, next: %p, %b%s", m2, m2->m_len, 10607b125090SJohn-Mark Gurney m2->m_next, m2->m_flags, "\20\20freelist\17skipfw" 10617b125090SJohn-Mark Gurney "\11proto5\10proto4\7proto3\6proto2\5proto1\4rdonly" 10627b125090SJohn-Mark Gurney "\3eor\2pkthdr\1ext", pdata ? "" : "\n"); 10637b125090SJohn-Mark Gurney if (pdata) 10647b125090SJohn-Mark Gurney printf(", %*D\n", m2->m_len, (u_char *)m2->m_data, "-"); 10657b125090SJohn-Mark Gurney if (len != -1) 1066ce4a64f7SPoul-Henning Kamp len -= m2->m_len; 1067ce4a64f7SPoul-Henning Kamp m2 = m2->m_next; 1068ce4a64f7SPoul-Henning Kamp } 10697b125090SJohn-Mark Gurney if (len > 0) 10707b125090SJohn-Mark Gurney printf("%d bytes unaccounted for.\n", len); 1071ce4a64f7SPoul-Henning Kamp return; 1072ce4a64f7SPoul-Henning Kamp } 10733f2e06c5SPoul-Henning Kamp 1074bd395ae8SBosko Milekic u_int 10753f2e06c5SPoul-Henning Kamp m_fixhdr(struct mbuf *m0) 10763f2e06c5SPoul-Henning Kamp { 1077bd395ae8SBosko Milekic u_int len; 10783f2e06c5SPoul-Henning Kamp 1079ac6e585dSPoul-Henning Kamp len = m_length(m0, NULL); 10803f2e06c5SPoul-Henning Kamp m0->m_pkthdr.len = len; 1081ac6e585dSPoul-Henning Kamp return (len); 1082ac6e585dSPoul-Henning Kamp } 1083ac6e585dSPoul-Henning Kamp 1084bd395ae8SBosko Milekic u_int 1085ac6e585dSPoul-Henning Kamp m_length(struct mbuf *m0, struct mbuf **last) 1086ac6e585dSPoul-Henning Kamp { 1087ac6e585dSPoul-Henning Kamp struct mbuf *m; 1088bd395ae8SBosko Milekic u_int len; 1089ac6e585dSPoul-Henning Kamp 1090ac6e585dSPoul-Henning Kamp len = 0; 1091ac6e585dSPoul-Henning Kamp for (m = m0; m != NULL; m = m->m_next) { 1092ac6e585dSPoul-Henning Kamp len += m->m_len; 1093ac6e585dSPoul-Henning Kamp if (m->m_next == NULL) 1094ac6e585dSPoul-Henning Kamp break; 1095ac6e585dSPoul-Henning Kamp } 1096ac6e585dSPoul-Henning Kamp if (last != NULL) 1097ac6e585dSPoul-Henning Kamp *last = m; 1098ac6e585dSPoul-Henning Kamp return (len); 10993f2e06c5SPoul-Henning Kamp } 110055e9f80dSMike Silbersack 110155e9f80dSMike Silbersack /* 110255e9f80dSMike Silbersack * Defragment a mbuf chain, returning the shortest possible 110355e9f80dSMike Silbersack * chain of mbufs and clusters. If allocation fails and 110455e9f80dSMike Silbersack * this cannot be completed, NULL will be returned, but 110555e9f80dSMike Silbersack * the passed in chain will be unchanged. Upon success, 110655e9f80dSMike Silbersack * the original chain will be freed, and the new chain 110755e9f80dSMike Silbersack * will be returned. 110855e9f80dSMike Silbersack * 110955e9f80dSMike Silbersack * If a non-packet header is passed in, the original 111055e9f80dSMike Silbersack * mbuf (chain?) will be returned unharmed. 111155e9f80dSMike Silbersack */ 111255e9f80dSMike Silbersack struct mbuf * 111355e9f80dSMike Silbersack m_defrag(struct mbuf *m0, int how) 111455e9f80dSMike Silbersack { 111555e9f80dSMike Silbersack struct mbuf *m_new = NULL, *m_final = NULL; 111655e9f80dSMike Silbersack int progress = 0, length; 111755e9f80dSMike Silbersack 1118063d8114SAlfred Perlstein MBUF_CHECKSLEEP(how); 111955e9f80dSMike Silbersack if (!(m0->m_flags & M_PKTHDR)) 112055e9f80dSMike Silbersack return (m0); 112155e9f80dSMike Silbersack 1122f8bf8e39SMike Silbersack m_fixhdr(m0); /* Needed sanity check */ 1123f8bf8e39SMike Silbersack 1124352d050eSMike Silbersack #ifdef MBUF_STRESS_TEST 1125352d050eSMike Silbersack if (m_defragrandomfailures) { 1126352d050eSMike Silbersack int temp = arc4random() & 0xff; 1127352d050eSMike Silbersack if (temp == 0xba) 1128352d050eSMike Silbersack goto nospace; 1129352d050eSMike Silbersack } 1130352d050eSMike Silbersack #endif 113155e9f80dSMike Silbersack 113255e9f80dSMike Silbersack if (m0->m_pkthdr.len > MHLEN) 113355e9f80dSMike Silbersack m_final = m_getcl(how, MT_DATA, M_PKTHDR); 113455e9f80dSMike Silbersack else 113555e9f80dSMike Silbersack m_final = m_gethdr(how, MT_DATA); 113655e9f80dSMike Silbersack 113755e9f80dSMike Silbersack if (m_final == NULL) 113855e9f80dSMike Silbersack goto nospace; 113955e9f80dSMike Silbersack 1140a89ec05eSPeter Wemm if (m_dup_pkthdr(m_final, m0, how) == 0) 114155e9f80dSMike Silbersack goto nospace; 114255e9f80dSMike Silbersack 114355e9f80dSMike Silbersack m_new = m_final; 114455e9f80dSMike Silbersack 114555e9f80dSMike Silbersack while (progress < m0->m_pkthdr.len) { 114655e9f80dSMike Silbersack length = m0->m_pkthdr.len - progress; 114755e9f80dSMike Silbersack if (length > MCLBYTES) 114855e9f80dSMike Silbersack length = MCLBYTES; 114955e9f80dSMike Silbersack 115055e9f80dSMike Silbersack if (m_new == NULL) { 115155e9f80dSMike Silbersack if (length > MLEN) 115255e9f80dSMike Silbersack m_new = m_getcl(how, MT_DATA, 0); 115355e9f80dSMike Silbersack else 115455e9f80dSMike Silbersack m_new = m_get(how, MT_DATA); 115555e9f80dSMike Silbersack if (m_new == NULL) 115655e9f80dSMike Silbersack goto nospace; 115755e9f80dSMike Silbersack } 115855e9f80dSMike Silbersack 115955e9f80dSMike Silbersack m_copydata(m0, progress, length, mtod(m_new, caddr_t)); 116055e9f80dSMike Silbersack progress += length; 116155e9f80dSMike Silbersack m_new->m_len = length; 116255e9f80dSMike Silbersack if (m_new != m_final) 116355e9f80dSMike Silbersack m_cat(m_final, m_new); 116455e9f80dSMike Silbersack m_new = NULL; 116555e9f80dSMike Silbersack } 116651710a45SMike Silbersack #ifdef MBUF_STRESS_TEST 116755e9f80dSMike Silbersack if (m0->m_next == NULL) 116855e9f80dSMike Silbersack m_defraguseless++; 116951710a45SMike Silbersack #endif 117055e9f80dSMike Silbersack m_freem(m0); 117155e9f80dSMike Silbersack m0 = m_final; 117251710a45SMike Silbersack #ifdef MBUF_STRESS_TEST 117355e9f80dSMike Silbersack m_defragpackets++; 117455e9f80dSMike Silbersack m_defragbytes += m0->m_pkthdr.len; 117551710a45SMike Silbersack #endif 117655e9f80dSMike Silbersack return (m0); 117755e9f80dSMike Silbersack nospace: 117851710a45SMike Silbersack #ifdef MBUF_STRESS_TEST 117955e9f80dSMike Silbersack m_defragfailure++; 118051710a45SMike Silbersack #endif 118155e9f80dSMike Silbersack if (m_new) 118255e9f80dSMike Silbersack m_free(m_new); 118355e9f80dSMike Silbersack if (m_final) 118455e9f80dSMike Silbersack m_freem(m_final); 118555e9f80dSMike Silbersack return (NULL); 118655e9f80dSMike Silbersack } 11873390d476SMike Silbersack 11883390d476SMike Silbersack #ifdef MBUF_STRESS_TEST 11893390d476SMike Silbersack 11903390d476SMike Silbersack /* 11913390d476SMike Silbersack * Fragment an mbuf chain. There's no reason you'd ever want to do 11923390d476SMike Silbersack * this in normal usage, but it's great for stress testing various 11933390d476SMike Silbersack * mbuf consumers. 11943390d476SMike Silbersack * 11953390d476SMike Silbersack * If fragmentation is not possible, the original chain will be 11963390d476SMike Silbersack * returned. 11973390d476SMike Silbersack * 11983390d476SMike Silbersack * Possible length values: 11993390d476SMike Silbersack * 0 no fragmentation will occur 12003390d476SMike Silbersack * > 0 each fragment will be of the specified length 12013390d476SMike Silbersack * -1 each fragment will be the same random value in length 12023390d476SMike Silbersack * -2 each fragment's length will be entirely random 12033390d476SMike Silbersack * (Random values range from 1 to 256) 12043390d476SMike Silbersack */ 12053390d476SMike Silbersack struct mbuf * 12063390d476SMike Silbersack m_fragment(struct mbuf *m0, int how, int length) 12073390d476SMike Silbersack { 12083390d476SMike Silbersack struct mbuf *m_new = NULL, *m_final = NULL; 12093390d476SMike Silbersack int progress = 0; 12103390d476SMike Silbersack 12113390d476SMike Silbersack if (!(m0->m_flags & M_PKTHDR)) 12123390d476SMike Silbersack return (m0); 12133390d476SMike Silbersack 12143390d476SMike Silbersack if ((length == 0) || (length < -2)) 12153390d476SMike Silbersack return (m0); 12163390d476SMike Silbersack 12173390d476SMike Silbersack m_fixhdr(m0); /* Needed sanity check */ 12183390d476SMike Silbersack 12193390d476SMike Silbersack m_final = m_getcl(how, MT_DATA, M_PKTHDR); 12203390d476SMike Silbersack 12213390d476SMike Silbersack if (m_final == NULL) 12223390d476SMike Silbersack goto nospace; 12233390d476SMike Silbersack 12248dee2f67SMike Silbersack if (m_dup_pkthdr(m_final, m0, how) == 0) 12253390d476SMike Silbersack goto nospace; 12263390d476SMike Silbersack 12273390d476SMike Silbersack m_new = m_final; 12283390d476SMike Silbersack 12293390d476SMike Silbersack if (length == -1) 12303390d476SMike Silbersack length = 1 + (arc4random() & 255); 12313390d476SMike Silbersack 12323390d476SMike Silbersack while (progress < m0->m_pkthdr.len) { 12333390d476SMike Silbersack int fraglen; 12343390d476SMike Silbersack 12353390d476SMike Silbersack if (length > 0) 12363390d476SMike Silbersack fraglen = length; 12373390d476SMike Silbersack else 12383390d476SMike Silbersack fraglen = 1 + (arc4random() & 255); 12393390d476SMike Silbersack if (fraglen > m0->m_pkthdr.len - progress) 12403390d476SMike Silbersack fraglen = m0->m_pkthdr.len - progress; 12413390d476SMike Silbersack 12423390d476SMike Silbersack if (fraglen > MCLBYTES) 12433390d476SMike Silbersack fraglen = MCLBYTES; 12443390d476SMike Silbersack 12453390d476SMike Silbersack if (m_new == NULL) { 12463390d476SMike Silbersack m_new = m_getcl(how, MT_DATA, 0); 12473390d476SMike Silbersack if (m_new == NULL) 12483390d476SMike Silbersack goto nospace; 12493390d476SMike Silbersack } 12503390d476SMike Silbersack 12513390d476SMike Silbersack m_copydata(m0, progress, fraglen, mtod(m_new, caddr_t)); 12523390d476SMike Silbersack progress += fraglen; 12533390d476SMike Silbersack m_new->m_len = fraglen; 12543390d476SMike Silbersack if (m_new != m_final) 12553390d476SMike Silbersack m_cat(m_final, m_new); 12563390d476SMike Silbersack m_new = NULL; 12573390d476SMike Silbersack } 12583390d476SMike Silbersack m_freem(m0); 12593390d476SMike Silbersack m0 = m_final; 12603390d476SMike Silbersack return (m0); 12613390d476SMike Silbersack nospace: 12623390d476SMike Silbersack if (m_new) 12633390d476SMike Silbersack m_free(m_new); 12643390d476SMike Silbersack if (m_final) 12653390d476SMike Silbersack m_freem(m_final); 12663390d476SMike Silbersack /* Return the original chain on failure */ 12673390d476SMike Silbersack return (m0); 12683390d476SMike Silbersack } 12693390d476SMike Silbersack 12703390d476SMike Silbersack #endif 1271beb699c7SMike Silbersack 1272beb699c7SMike Silbersack struct mbuf * 1273beb699c7SMike Silbersack m_uiotombuf(struct uio *uio, int how, int len) 1274beb699c7SMike Silbersack { 1275beb699c7SMike Silbersack struct mbuf *m_new = NULL, *m_final = NULL; 1276beb699c7SMike Silbersack int progress = 0, error = 0, length, total; 1277beb699c7SMike Silbersack 1278beb699c7SMike Silbersack if (len > 0) 1279beb699c7SMike Silbersack total = min(uio->uio_resid, len); 1280beb699c7SMike Silbersack else 1281beb699c7SMike Silbersack total = uio->uio_resid; 1282beb699c7SMike Silbersack if (total > MHLEN) 1283beb699c7SMike Silbersack m_final = m_getcl(how, MT_DATA, M_PKTHDR); 1284beb699c7SMike Silbersack else 1285beb699c7SMike Silbersack m_final = m_gethdr(how, MT_DATA); 1286beb699c7SMike Silbersack if (m_final == NULL) 1287beb699c7SMike Silbersack goto nospace; 1288beb699c7SMike Silbersack m_new = m_final; 1289beb699c7SMike Silbersack while (progress < total) { 1290beb699c7SMike Silbersack length = total - progress; 1291beb699c7SMike Silbersack if (length > MCLBYTES) 1292beb699c7SMike Silbersack length = MCLBYTES; 1293beb699c7SMike Silbersack if (m_new == NULL) { 1294beb699c7SMike Silbersack if (length > MLEN) 1295beb699c7SMike Silbersack m_new = m_getcl(how, MT_DATA, 0); 1296beb699c7SMike Silbersack else 1297beb699c7SMike Silbersack m_new = m_get(how, MT_DATA); 1298beb699c7SMike Silbersack if (m_new == NULL) 1299beb699c7SMike Silbersack goto nospace; 1300beb699c7SMike Silbersack } 1301beb699c7SMike Silbersack error = uiomove(mtod(m_new, void *), length, uio); 1302beb699c7SMike Silbersack if (error) 1303beb699c7SMike Silbersack goto nospace; 1304beb699c7SMike Silbersack progress += length; 1305beb699c7SMike Silbersack m_new->m_len = length; 1306beb699c7SMike Silbersack if (m_new != m_final) 1307beb699c7SMike Silbersack m_cat(m_final, m_new); 1308beb699c7SMike Silbersack m_new = NULL; 1309beb699c7SMike Silbersack } 1310beb699c7SMike Silbersack m_fixhdr(m_final); 1311beb699c7SMike Silbersack return (m_final); 1312beb699c7SMike Silbersack nospace: 1313beb699c7SMike Silbersack if (m_new) 1314beb699c7SMike Silbersack m_free(m_new); 1315beb699c7SMike Silbersack if (m_final) 1316beb699c7SMike Silbersack m_freem(m_final); 1317beb699c7SMike Silbersack return (NULL); 1318beb699c7SMike Silbersack } 1319