The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/uipc_mbuf2.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: uipc_mbuf2.c,v 1.16.2.1 2005/05/06 23:50:22 snj Exp $  */
    2 /*      $KAME: uipc_mbuf2.c,v 1.29 2001/02/14 13:42:10 itojun Exp $     */
    3 
    4 /*
    5  * Copyright (C) 1999 WIDE Project.
    6  * All rights reserved.
    7  * 
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 3. Neither the name of the project nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  * 
   20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  */
   32 
   33 /*
   34  * Copyright (c) 1982, 1986, 1988, 1991, 1993
   35  *      The Regents of the University of California.  All rights reserved.
   36  *
   37  * Redistribution and use in source and binary forms, with or without
   38  * modification, are permitted provided that the following conditions
   39  * are met:
   40  * 1. Redistributions of source code must retain the above copyright
   41  *    notice, this list of conditions and the following disclaimer.
   42  * 2. Redistributions in binary form must reproduce the above copyright
   43  *    notice, this list of conditions and the following disclaimer in the
   44  *    documentation and/or other materials provided with the distribution.
   45  * 3. Neither the name of the University nor the names of its contributors
   46  *    may be used to endorse or promote products derived from this software
   47  *    without specific prior written permission.
   48  *
   49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   59  * SUCH DAMAGE.
   60  *
   61  *      @(#)uipc_mbuf.c 8.4 (Berkeley) 2/14/95
   62  */
   63 
   64 #include <sys/cdefs.h>
   65 __KERNEL_RCSID(0, "$NetBSD: uipc_mbuf2.c,v 1.16.2.1 2005/05/06 23:50:22 snj Exp $");
   66 
   67 #include <sys/param.h>
   68 #include <sys/systm.h>
   69 #include <sys/proc.h>
   70 #include <sys/malloc.h>
   71 #include <sys/mbuf.h>
   72 
   73 MALLOC_DEFINE(M_PACKET_TAGS, "packet tags", "Packet-attached information");
   74 
   75 /*
   76  * ensure that [off, off + len) is contiguous on the mbuf chain "m".
   77  * packet chain before "off" is kept untouched.
   78  * if offp == NULL, the target will start at <retval, 0> on resulting chain.
   79  * if offp != NULL, the target will start at <retval, *offp> on resulting chain.
   80  *
   81  * on error return (NULL return value), original "m" will be freed.
   82  *
   83  * XXX M_TRAILINGSPACE/M_LEADINGSPACE on shared cluster (sharedcluster)
   84  */
   85 struct mbuf *
   86 m_pulldown(struct mbuf *m, int off, int len, int *offp)
   87 {
   88         struct mbuf *n, *o;
   89         int hlen, tlen, olen;
   90         int sharedcluster;
   91 
   92         /* check invalid arguments. */
   93         if (m == NULL)
   94                 panic("m == NULL in m_pulldown()");
   95         if (len > MCLBYTES) {
   96                 m_freem(m);
   97                 return NULL;    /* impossible */
   98         }
   99 
  100         n = m;
  101         while (n != NULL && off > 0) {
  102                 if (n->m_len > off)
  103                         break;
  104                 off -= n->m_len;
  105                 n = n->m_next;
  106         }
  107         /* be sure to point non-empty mbuf */
  108         while (n != NULL && n->m_len == 0)
  109                 n = n->m_next;
  110         if (!n) {
  111                 m_freem(m);
  112                 return NULL;    /* mbuf chain too short */
  113         }
  114 
  115         sharedcluster = M_READONLY(n);
  116 
  117         /*
  118          * the target data is on <n, off>.
  119          * if we got enough data on the mbuf "n", we're done.
  120          */
  121         if ((off == 0 || offp) && len <= n->m_len - off && !sharedcluster)
  122                 goto ok;
  123 
  124         /*
  125          * when len <= n->m_len - off and off != 0, it is a special case.
  126          * len bytes from <n, off> sits in single mbuf, but the caller does
  127          * not like the starting position (off).
  128          * chop the current mbuf into two pieces, set off to 0.
  129          */
  130         if (len <= n->m_len - off) {
  131                 o = m_dup(n, off, n->m_len - off, M_DONTWAIT);
  132                 if (o == NULL) {
  133                         m_freem(m);
  134                         return NULL;    /* ENOBUFS */
  135                 }
  136                 n->m_len = off;
  137                 o->m_next = n->m_next;
  138                 n->m_next = o;
  139                 n = n->m_next;
  140                 off = 0;
  141                 goto ok;
  142         }
  143 
  144         /*
  145          * we need to take hlen from <n, off> and tlen from <n->m_next, 0>,
  146          * and construct contiguous mbuf with m_len == len.
  147          * note that hlen + tlen == len, and tlen > 0.
  148          */
  149         hlen = n->m_len - off;
  150         tlen = len - hlen;
  151 
  152         /*
  153          * ensure that we have enough trailing data on mbuf chain.
  154          * if not, we can do nothing about the chain.
  155          */
  156         olen = 0;
  157         for (o = n->m_next; o != NULL; o = o->m_next)
  158                 olen += o->m_len;
  159         if (hlen + olen < len) {
  160                 m_freem(m);
  161                 return NULL;    /* mbuf chain too short */
  162         }
  163 
  164         /*
  165          * easy cases first.
  166          * we need to use m_copydata() to get data from <n->m_next, 0>.
  167          */
  168         if ((off == 0 || offp) && M_TRAILINGSPACE(n) >= tlen &&
  169             !sharedcluster) {
  170                 m_copydata(n->m_next, 0, tlen, mtod(n, caddr_t) + n->m_len);
  171                 n->m_len += tlen;
  172                 m_adj(n->m_next, tlen);
  173                 goto ok;
  174         }
  175         if ((off == 0 || offp) && M_LEADINGSPACE(n->m_next) >= hlen &&
  176             !sharedcluster && n->m_next->m_len >= tlen) {
  177                 n->m_next->m_data -= hlen;
  178                 n->m_next->m_len += hlen;
  179                 memcpy(mtod(n->m_next, caddr_t), mtod(n, caddr_t) + off, hlen);
  180                 n->m_len -= hlen;
  181                 n = n->m_next;
  182                 off = 0;
  183                 goto ok;
  184         }
  185 
  186         /*
  187          * now, we need to do the hard way.  don't m_copy as there's no room
  188          * on both end.
  189          */
  190         MGET(o, M_DONTWAIT, m->m_type);
  191         if (o && len > MLEN) {
  192                 MCLGET(o, M_DONTWAIT);
  193                 if ((o->m_flags & M_EXT) == 0) {
  194                         m_free(o);
  195                         o = NULL;
  196                 }
  197         }
  198         if (!o) {
  199                 m_freem(m);
  200                 return NULL;    /* ENOBUFS */
  201         }
  202         /* get hlen from <n, off> into <o, 0> */
  203         o->m_len = hlen;
  204         memcpy(mtod(o, caddr_t), mtod(n, caddr_t) + off, hlen);
  205         n->m_len -= hlen;
  206         /* get tlen from <n->m_next, 0> into <o, hlen> */
  207         m_copydata(n->m_next, 0, tlen, mtod(o, caddr_t) + o->m_len);
  208         o->m_len += tlen;
  209         m_adj(n->m_next, tlen);
  210         o->m_next = n->m_next;
  211         n->m_next = o;
  212         n = o;
  213         off = 0;
  214 
  215 ok:
  216         if (offp)
  217                 *offp = off;
  218         return n;
  219 }
  220 
  221 /* Get a packet tag structure along with specified data following. */
  222 struct m_tag *
  223 m_tag_get(int type, int len, int wait)
  224 {
  225         struct m_tag *t;
  226 
  227         if (len < 0)
  228                 return (NULL);
  229         t = malloc(len + sizeof(struct m_tag), M_PACKET_TAGS, wait);
  230         if (t == NULL)
  231                 return (NULL);
  232         t->m_tag_id = type;
  233         t->m_tag_len = len;
  234         return (t);
  235 }
  236 
  237 /* Free a packet tag. */
  238 void
  239 m_tag_free(struct m_tag *t)
  240 {
  241 
  242         free(t, M_PACKET_TAGS);
  243 }
  244 
  245 /* Prepend a packet tag. */
  246 void
  247 m_tag_prepend(struct mbuf *m, struct m_tag *t)
  248 {
  249 
  250         SLIST_INSERT_HEAD(&m->m_pkthdr.tags, t, m_tag_link);
  251 }
  252 
  253 /* Unlink a packet tag. */
  254 void
  255 m_tag_unlink(struct mbuf *m, struct m_tag *t)
  256 {
  257 
  258         SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link);
  259 }
  260 
  261 /* Unlink and free a packet tag. */
  262 void
  263 m_tag_delete(struct mbuf *m, struct m_tag *t)
  264 {
  265 
  266         m_tag_unlink(m, t);
  267         m_tag_free(t);
  268 }
  269 
  270 /* Unlink and free a packet tag chain, starting from given tag. */
  271 __inline void
  272 m_tag_delete_chain(struct mbuf *m, struct m_tag *t)
  273 {
  274         struct m_tag *p, *q;
  275 
  276         if (t != NULL)
  277                 p = t;
  278         else
  279                 p = SLIST_FIRST(&m->m_pkthdr.tags);
  280         if (p == NULL)
  281                 return;
  282         while ((q = SLIST_NEXT(p, m_tag_link)) != NULL)
  283                 m_tag_delete(m, q);
  284         m_tag_delete(m, p);
  285 }
  286 
  287 /*
  288  * Strip off all tags that would normally vanish when
  289  * passing through a network interface.  Only persistent
  290  * tags will exist after this; these are expected to remain
  291  * so long as the mbuf chain exists, regardless of the
  292  * path the mbufs take.
  293  */
  294 void
  295 m_tag_delete_nonpersistent(struct mbuf *m)
  296 {
  297         /* NetBSD has no persistent tags yet, so just delete all tags. */
  298         return m_tag_delete_chain(m, NULL);
  299 }
  300 
  301 
  302 /* Find a tag, starting from a given position. */
  303 struct m_tag *
  304 m_tag_find(struct mbuf *m, int type, struct m_tag *t)
  305 {
  306         struct m_tag *p;
  307 
  308         if (t == NULL)
  309                 p = SLIST_FIRST(&m->m_pkthdr.tags);
  310         else
  311                 p = SLIST_NEXT(t, m_tag_link);
  312         while (p != NULL) {
  313                 if (p->m_tag_id == type)
  314                         return (p);
  315                 p = SLIST_NEXT(p, m_tag_link);
  316         }
  317         return (NULL);
  318 }
  319 
  320 /* Copy a single tag. */
  321 struct m_tag *
  322 m_tag_copy(struct m_tag *t)
  323 {
  324         struct m_tag *p;
  325 
  326         p = m_tag_get(t->m_tag_id, t->m_tag_len, M_NOWAIT);
  327         if (p == NULL)
  328                 return (NULL);
  329         bcopy(t + 1, p + 1, t->m_tag_len); /* Copy the data */
  330         return (p);
  331 }
  332 
  333 /*
  334  * Copy two tag chains. The destination mbuf (to) loses any attached
  335  * tags even if the operation fails. This should not be a problem, as
  336  * m_tag_copy_chain() is typically called with a newly-allocated
  337  * destination mbuf.
  338  */
  339 int
  340 m_tag_copy_chain(struct mbuf *to, struct mbuf *from)
  341 {
  342         struct m_tag *p, *t, *tprev = NULL;
  343 
  344         m_tag_delete_chain(to, NULL);
  345         SLIST_FOREACH(p, &from->m_pkthdr.tags, m_tag_link) {
  346                 t = m_tag_copy(p);
  347                 if (t == NULL) {
  348                         m_tag_delete_chain(to, NULL);
  349                         return (0);
  350                 }
  351                 if (tprev == NULL)
  352                         SLIST_INSERT_HEAD(&to->m_pkthdr.tags, t, m_tag_link);
  353                 else
  354                         SLIST_INSERT_AFTER(tprev, t, m_tag_link);
  355                 tprev = t;
  356         }
  357         return (1);
  358 }
  359 
  360 /* Initialize tags on an mbuf. */
  361 void
  362 m_tag_init(struct mbuf *m)
  363 {
  364 
  365         SLIST_INIT(&m->m_pkthdr.tags);
  366 }
  367 
  368 /* Get first tag in chain. */
  369 struct m_tag *
  370 m_tag_first(struct mbuf *m)
  371 {
  372 
  373         return (SLIST_FIRST(&m->m_pkthdr.tags));
  374 }
  375 
  376 /* Get next tag in chain. */
  377 struct m_tag *
  378 m_tag_next(struct mbuf *m, struct m_tag *t)
  379 {
  380 
  381         return (SLIST_NEXT(t, m_tag_link));
  382 }

Cache object: 152bedb91731cdfed97c9621d34d87cb


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.