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/sys/mbuf.h

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 /*-
    2  * Copyright (c) 1982, 1986, 1988, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. Neither the name of the University nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      @(#)mbuf.h      8.5 (Berkeley) 2/19/95
   30  * $FreeBSD$
   31  */
   32 
   33 #ifndef _SYS_MBUF_H_
   34 #define _SYS_MBUF_H_
   35 
   36 /* XXX: These includes suck. Sorry! */
   37 #include <sys/queue.h>
   38 #ifdef _KERNEL
   39 #include <sys/systm.h>
   40 #include <vm/uma.h>
   41 #ifdef WITNESS
   42 #include <sys/lock.h>
   43 #endif
   44 #endif
   45 
   46 /*
   47  * Mbufs are of a single size, MSIZE (sys/param.h), which
   48  * includes overhead.  An mbuf may add a single "mbuf cluster" of size
   49  * MCLBYTES (also in sys/param.h), which has no additional overhead
   50  * and is used instead of the internal data area; this is done when
   51  * at least MINCLSIZE of data must be stored.  Additionally, it is possible
   52  * to allocate a separate buffer externally and attach it to the mbuf in
   53  * a way similar to that of mbuf clusters.
   54  */
   55 #define MLEN            (MSIZE - sizeof(struct m_hdr))  /* normal data len */
   56 #define MHLEN           (MLEN - sizeof(struct pkthdr))  /* data len w/pkthdr */
   57 #define MINCLSIZE       (MHLEN + 1)     /* smallest amount to put in cluster */
   58 #define M_MAXCOMPRESS   (MHLEN / 2)     /* max amount to copy for compression */
   59 
   60 #ifdef _KERNEL
   61 /*-
   62  * Macros for type conversion:
   63  * mtod(m, t)   -- Convert mbuf pointer to data pointer of correct type.
   64  * dtom(x)      -- Convert data pointer within mbuf to mbuf pointer (XXX).
   65  */
   66 #define mtod(m, t)      ((t)((m)->m_data))
   67 #define dtom(x)         ((struct mbuf *)((intptr_t)(x) & ~(MSIZE-1)))
   68 
   69 /*
   70  * Argument structure passed to UMA routines during mbuf and packet
   71  * allocations.
   72  */
   73 struct mb_args {
   74         int     flags;  /* Flags for mbuf being allocated */
   75         short   type;   /* Type of mbuf being allocated */
   76 };
   77 #endif /* _KERNEL */
   78 
   79 /*
   80  * Header present at the beginning of every mbuf.
   81  */
   82 struct m_hdr {
   83         struct  mbuf *mh_next;          /* next buffer in chain */
   84         struct  mbuf *mh_nextpkt;       /* next chain in queue/record */
   85         caddr_t mh_data;                /* location of data */
   86         int     mh_len;                 /* amount of data in this mbuf */
   87         int     mh_flags;               /* flags; see below */
   88         short   mh_type;                /* type of data in this mbuf */
   89 };
   90 
   91 /*
   92  * Packet tag structure (see below for details).
   93  */
   94 struct m_tag {
   95         SLIST_ENTRY(m_tag)      m_tag_link;     /* List of packet tags */
   96         u_int16_t               m_tag_id;       /* Tag ID */
   97         u_int16_t               m_tag_len;      /* Length of data */
   98         u_int32_t               m_tag_cookie;   /* ABI/Module ID */
   99         void                    (*m_tag_free)(struct m_tag *);
  100 };
  101 
  102 /*
  103  * Record/packet header in first mbuf of chain; valid only if M_PKTHDR is set.
  104  */
  105 struct pkthdr {
  106         struct  ifnet *rcvif;           /* rcv interface */
  107         int     len;                    /* total packet length */
  108         /* variables for ip and tcp reassembly */
  109         void    *header;                /* pointer to packet header */
  110         /* variables for hardware checksum */
  111         int     csum_flags;             /* flags regarding checksum */
  112         int     csum_data;              /* data field used by csum routines */
  113         SLIST_HEAD(packet_tags, m_tag) tags; /* list of packet tags */
  114 };
  115 
  116 /*
  117  * Description of external storage mapped into mbuf; valid only if M_EXT is set.
  118  */
  119 struct m_ext {
  120         caddr_t ext_buf;                /* start of buffer */
  121         void    (*ext_free)             /* free routine if not the usual */
  122                     (void *, void *);
  123         void    *ext_args;              /* optional argument pointer */
  124         u_int   ext_size;               /* size of buffer, for ext_free */
  125         volatile u_int *ref_cnt;        /* pointer to ref count info */
  126         int     ext_type;               /* type of external storage */
  127 };
  128 
  129 /*
  130  * The core of the mbuf object along with some shortcut defines for
  131  * practical purposes.
  132  */
  133 struct mbuf {
  134         struct  m_hdr m_hdr;
  135         union {
  136                 struct {
  137                         struct  pkthdr MH_pkthdr;       /* M_PKTHDR set */
  138                         union {
  139                                 struct  m_ext MH_ext;   /* M_EXT set */
  140                                 char    MH_databuf[MHLEN];
  141                         } MH_dat;
  142                 } MH;
  143                 char    M_databuf[MLEN];                /* !M_PKTHDR, !M_EXT */
  144         } M_dat;
  145 };
  146 #define m_next          m_hdr.mh_next
  147 #define m_len           m_hdr.mh_len
  148 #define m_data          m_hdr.mh_data
  149 #define m_type          m_hdr.mh_type
  150 #define m_flags         m_hdr.mh_flags
  151 #define m_nextpkt       m_hdr.mh_nextpkt
  152 #define m_act           m_nextpkt
  153 #define m_pkthdr        M_dat.MH.MH_pkthdr
  154 #define m_ext           M_dat.MH.MH_dat.MH_ext
  155 #define m_pktdat        M_dat.MH.MH_dat.MH_databuf
  156 #define m_dat           M_dat.M_databuf
  157 
  158 /*
  159  * mbuf flags.
  160  */
  161 #define M_EXT           0x0001  /* has associated external storage */
  162 #define M_PKTHDR        0x0002  /* start of record */
  163 #define M_EOR           0x0004  /* end of record */
  164 #define M_RDONLY        0x0008  /* associated data is marked read-only */
  165 #define M_PROTO1        0x0010  /* protocol-specific */
  166 #define M_PROTO2        0x0020  /* protocol-specific */
  167 #define M_PROTO3        0x0040  /* protocol-specific */
  168 #define M_PROTO4        0x0080  /* protocol-specific */
  169 #define M_PROTO5        0x0100  /* protocol-specific */
  170 #define M_SKIP_FIREWALL 0x4000  /* skip firewall processing */
  171 #define M_FREELIST      0x8000  /* mbuf is on the free list */
  172 
  173 /*
  174  * mbuf pkthdr flags (also stored in m_flags).
  175  */
  176 #define M_BCAST         0x0200  /* send/received as link-level broadcast */
  177 #define M_MCAST         0x0400  /* send/received as link-level multicast */
  178 #define M_FRAG          0x0800  /* packet is a fragment of a larger packet */
  179 #define M_FIRSTFRAG     0x1000  /* packet is first fragment */
  180 #define M_LASTFRAG      0x2000  /* packet is last fragment */
  181 #define M_VLANTAG       0x10000 /* packet has VLAN tag attached */
  182 
  183 /*
  184  * External buffer types: identify ext_buf type.
  185  */
  186 #define EXT_CLUSTER     1       /* mbuf cluster */
  187 #define EXT_SFBUF       2       /* sendfile(2)'s sf_bufs */
  188 #define EXT_PACKET      3       /* came out of Packet zone */
  189 #define EXT_JUMBOP      4       /* jumbo cluster 4096 bytes */
  190 #define EXT_JUMBO9      5       /* jumbo cluster 9216 bytes */
  191 #define EXT_JUMBO16     6       /* jumbo cluster 16184 bytes */
  192 #define EXT_NET_DRV     100     /* custom ext_buf provided by net driver(s) */
  193 #define EXT_MOD_TYPE    200     /* custom module's ext_buf type */
  194 #define EXT_DISPOSABLE  300     /* can throw this buffer away w/page flipping */
  195 #define EXT_EXTREF      400     /* has externally maintained ref_cnt ptr */
  196 
  197 /*
  198  * Flags copied when copying m_pkthdr.
  199  */
  200 #define M_COPYFLAGS     (M_PKTHDR|M_EOR|M_RDONLY|M_PROTO1|M_PROTO1|M_PROTO2|\
  201                             M_PROTO3|M_PROTO4|M_PROTO5|M_SKIP_FIREWALL|\
  202                             M_BCAST|M_MCAST|M_FRAG|M_FIRSTFRAG|M_LASTFRAG|\
  203                             M_VLANTAG)
  204 
  205 /*
  206  * Flags indicating hw checksum support and sw checksum requirements.
  207  */
  208 #define CSUM_IP                 0x0001          /* will csum IP */
  209 #define CSUM_TCP                0x0002          /* will csum TCP */
  210 #define CSUM_UDP                0x0004          /* will csum UDP */
  211 #define CSUM_IP_FRAGS           0x0008          /* will csum IP fragments */
  212 #define CSUM_FRAGMENT           0x0010          /* will do IP fragmentation */
  213 
  214 #define CSUM_IP_CHECKED         0x0100          /* did csum IP */
  215 #define CSUM_IP_VALID           0x0200          /*   ... the csum is valid */
  216 #define CSUM_DATA_VALID         0x0400          /* csum_data field is valid */
  217 #define CSUM_PSEUDO_HDR         0x0800          /* csum_data has pseudo hdr */
  218 
  219 #define CSUM_DELAY_DATA         (CSUM_TCP | CSUM_UDP)
  220 #define CSUM_DELAY_IP           (CSUM_IP)       /* XXX add ipv6 here too? */
  221 
  222 /*
  223  * mbuf types.
  224  */
  225 #define MT_NOTMBUF      0       /* USED INTERNALLY ONLY! Object is not mbuf */
  226 #define MT_DATA         1       /* dynamic (data) allocation */
  227 #define MT_HEADER       2       /* packet header */
  228 #if 0
  229 #define MT_SOCKET       3       /* socket structure */
  230 #define MT_PCB          4       /* protocol control block */
  231 #define MT_RTABLE       5       /* routing tables */
  232 #define MT_HTABLE       6       /* IMP host tables */
  233 #define MT_ATABLE       7       /* address resolution tables */
  234 #endif
  235 #define MT_SONAME       8       /* socket name */
  236 #if 0
  237 #define MT_SOOPTS       10      /* socket options */
  238 #endif
  239 #define MT_FTABLE       11      /* fragment reassembly header */
  240 #if 0
  241 #define MT_RIGHTS       12      /* access rights */
  242 #define MT_IFADDR       13      /* interface address */
  243 #endif
  244 #define MT_CONTROL      14      /* extra-data protocol message */
  245 #define MT_OOBDATA      15      /* expedited data  */
  246 #define MT_NTYPES       16      /* number of mbuf types for mbtypes[] */
  247 
  248 #define MT_NOINIT       255     /* Not a type but a flag to allocate
  249                                    a non-initialized mbuf */
  250 
  251 /*
  252  * General mbuf allocator statistics structure.
  253  */
  254 struct mbstat {
  255         u_long  m_mbufs;        /* XXX */
  256         u_long  m_mclusts;      /* XXX */
  257 
  258         u_long  m_drain;        /* times drained protocols for space */
  259         u_long  m_mcfail;       /* XXX: times m_copym failed */
  260         u_long  m_mpfail;       /* XXX: times m_pullup failed */
  261         u_long  m_msize;        /* length of an mbuf */
  262         u_long  m_mclbytes;     /* length of an mbuf cluster */
  263         u_long  m_minclsize;    /* min length of data to allocate a cluster */
  264         u_long  m_mlen;         /* length of data in an mbuf */
  265         u_long  m_mhlen;        /* length of data in a header mbuf */
  266 
  267         /* Number of mbtypes (gives # elems in mbtypes[] array: */
  268         short   m_numtypes;
  269 
  270         /* XXX: Sendfile stats should eventually move to their own struct */
  271         u_long  sf_iocnt;       /* times sendfile had to do disk I/O */
  272         u_long  sf_allocfail;   /* times sfbuf allocation failed */
  273         u_long  sf_allocwait;   /* times sfbuf allocation had to wait */
  274 };
  275 
  276 /*
  277  * Flags specifying how an allocation should be made.
  278  *
  279  * The flag to use is as follows:
  280  * - M_DONTWAIT or M_NOWAIT from an interrupt handler to not block allocation.
  281  * - M_WAIT or M_WAITOK or M_TRYWAIT from wherever it is safe to block.
  282  *
  283  * M_DONTWAIT/M_NOWAIT means that we will not block the thread explicitly
  284  * and if we cannot allocate immediately we may return NULL,
  285  * whereas M_WAIT/M_WAITOK/M_TRYWAIT means that if we cannot allocate
  286  * resources we will block until they are available, and thus never
  287  * return NULL.
  288  *
  289  * XXX Eventually just phase this out to use M_WAITOK/M_NOWAIT.
  290  */
  291 #define MBTOM(how)      (how)
  292 #define M_DONTWAIT      M_NOWAIT
  293 #define M_TRYWAIT       M_WAITOK
  294 #define M_WAIT          M_WAITOK
  295 
  296 /*
  297  * String names of mbuf-related UMA(9) and malloc(9) types.  Exposed to
  298  * !_KERNEL so that monitoring tools can look up the zones with
  299  * libmemstat(3).
  300  */
  301 #define MBUF_MEM_NAME           "mbuf"
  302 #define MBUF_CLUSTER_MEM_NAME   "mbuf_cluster"
  303 #define MBUF_JUMBOP_MEM_NAME    "mbuf_jumbo_pagesize"
  304 #define MBUF_JUMBO9_MEM_NAME    "mbuf_jumbo_9k"
  305 #define MBUF_JUMBO16_MEM_NAME   "mbuf_jumbo_16k"
  306 #define MBUF_PACKET_MEM_NAME    "mbuf_packet"
  307 #define MBUF_TAG_MEM_NAME       "mbuf_tag"
  308 
  309 #ifdef _KERNEL
  310 /*-
  311  * mbuf external reference count management macros.
  312  *
  313  * MEXT_IS_REF(m): true if (m) is not the only mbuf referencing
  314  *     the external buffer ext_buf.
  315  *
  316  * MEXT_REM_REF(m): remove reference to m_ext object.
  317  *
  318  * MEXT_ADD_REF(m): add reference to m_ext object already
  319  *     referred to by (m).  XXX Note that it is VERY important that you
  320  *     always set the second mbuf's m_ext.ref_cnt to point to the first
  321  *     one's (i.e., n->m_ext.ref_cnt = m->m_ext.ref_cnt) AFTER you run
  322  *     MEXT_ADD_REF(m).  This is because m might have a lazy initialized
  323  *     ref_cnt (NULL) before this is run and it will only be looked up
  324  *     from here.  We should make MEXT_ADD_REF() always take two mbufs
  325  *     as arguments so that it can take care of this itself.
  326  */
  327 #define MEXT_IS_REF(m)  (((m)->m_ext.ref_cnt != NULL)                   \
  328     && (*((m)->m_ext.ref_cnt) > 1))
  329 
  330 #define MEXT_REM_REF(m) do {                                            \
  331         KASSERT((m)->m_ext.ref_cnt != NULL, ("m_ext refcnt lazy NULL")); \
  332         KASSERT(*((m)->m_ext.ref_cnt) > 0, ("m_ext refcnt < 0"));       \
  333         atomic_subtract_int((m)->m_ext.ref_cnt, 1);                     \
  334 } while(0)
  335 
  336 #define MEXT_ADD_REF(m) do {                                            \
  337         if ((m)->m_ext.ref_cnt == NULL) {                               \
  338                 KASSERT((m)->m_ext.ext_type == EXT_CLUSTER ||           \
  339                     (m)->m_ext.ext_type == EXT_PACKET,                  \
  340                     ("Unexpected mbuf type has lazy refcnt"));          \
  341                 (m)->m_ext.ref_cnt = (u_int *)uma_find_refcnt(          \
  342                     zone_clust, (m)->m_ext.ext_buf);                    \
  343                 *((m)->m_ext.ref_cnt) = 2;                              \
  344         } else                                                          \
  345                 atomic_add_int((m)->m_ext.ref_cnt, 1);                  \
  346 } while (0)
  347 
  348 #ifdef WITNESS
  349 #define MBUF_CHECKSLEEP(how) do {                                       \
  350         if (how == M_WAITOK)                                            \
  351                 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,         \
  352                     "Sleeping in \"%s\"", __func__);                    \
  353 } while(0)
  354 #else
  355 #define MBUF_CHECKSLEEP(how)
  356 #endif
  357 
  358 /*
  359  * Network buffer allocation API
  360  *
  361  * The rest of it is defined in kern/kern_mbuf.c
  362  */
  363 
  364 extern uma_zone_t       zone_mbuf;
  365 extern uma_zone_t       zone_clust;
  366 extern uma_zone_t       zone_pack;
  367 extern uma_zone_t       zone_jumbop;
  368 extern uma_zone_t       zone_jumbo9;
  369 extern uma_zone_t       zone_jumbo16;
  370 
  371 static __inline struct mbuf     *m_get(int how, short type);
  372 static __inline struct mbuf     *m_gethdr(int how, short type);
  373 static __inline struct mbuf     *m_getcl(int how, short type, int flags);
  374 static __inline struct mbuf     *m_getjcl(int how, short type, int flags, int size);
  375 static __inline struct mbuf     *m_getclr(int how, short type); /* XXX */
  376 static __inline struct mbuf     *m_free(struct mbuf *m);
  377 static __inline void             m_clget(struct mbuf *m, int how);
  378 static __inline void            *m_cljget(struct mbuf *m, int how, int size);
  379 static __inline void             m_chtype(struct mbuf *m, short new_type);
  380 void                             mb_free_ext(struct mbuf *);
  381 
  382 static __inline
  383 struct mbuf *
  384 m_get(int how, short type)
  385 {
  386         struct mb_args args;
  387 
  388         args.flags = 0;
  389         args.type = type;
  390         return (uma_zalloc_arg(zone_mbuf, &args, how));
  391 }
  392 
  393 /* XXX This should be depracated, very little use */
  394 static __inline
  395 struct mbuf *
  396 m_getclr(int how, short type)
  397 {
  398         struct mbuf *m;
  399         struct mb_args args;
  400 
  401         args.flags = 0;
  402         args.type = type;
  403         m = uma_zalloc_arg(zone_mbuf, &args, how);
  404         if (m != NULL)
  405                 bzero(m->m_data, MLEN);
  406         return m;
  407 }
  408 
  409 static __inline
  410 struct mbuf *
  411 m_gethdr(int how, short type)
  412 {
  413         struct mb_args args;
  414 
  415         args.flags = M_PKTHDR;
  416         args.type = type;
  417         return (uma_zalloc_arg(zone_mbuf, &args, how));
  418 }
  419 
  420 static __inline
  421 struct mbuf *
  422 m_getcl(int how, short type, int flags)
  423 {
  424         struct mb_args args;
  425 
  426         args.flags = flags;
  427         args.type = type;
  428         return (uma_zalloc_arg(zone_pack, &args, how));
  429 }
  430 
  431 /*
  432  * m_getjcl() returns an mbuf with a cluster of the specified size attached.
  433  * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES.
  434  */
  435 static __inline /* XXX: This is rather large, should be real function maybe. */
  436 struct mbuf *
  437 m_getjcl(int how, short type, int flags, int size)
  438 {
  439         struct mb_args args;
  440         struct mbuf *m, *n;
  441         uma_zone_t zone;
  442 
  443         args.flags = flags;
  444         args.type = type;
  445 
  446         m = uma_zalloc_arg(zone_mbuf, &args, how);
  447         if (m == NULL)
  448                 return NULL;
  449 
  450         switch (size) {
  451         case MCLBYTES:
  452                 zone = zone_clust;
  453                 break;
  454 #if MJUMPAGESIZE != MCLBYTES
  455         case MJUMPAGESIZE:
  456                 zone = zone_jumbop;
  457                 break;
  458 #endif
  459         case MJUM9BYTES:
  460                 zone = zone_jumbo9;
  461                 break;
  462         case MJUM16BYTES:
  463                 zone = zone_jumbo16;
  464                 break;
  465         default:
  466                 panic("%s: m_getjcl: invalid cluster type", __func__);
  467         }
  468         n = uma_zalloc_arg(zone, m, how);
  469         if (n == NULL) {
  470                 uma_zfree(zone_mbuf, m);
  471                 return NULL;
  472         }
  473         return m;
  474 }
  475 
  476 static __inline
  477 struct mbuf *
  478 m_free(struct mbuf *m)
  479 {
  480         struct mbuf *n = m->m_next;
  481 
  482 #ifdef INVARIANTS
  483         m->m_flags |= M_FREELIST;
  484 #endif
  485         if (m->m_flags & M_EXT)
  486                 mb_free_ext(m);
  487         else
  488                 uma_zfree(zone_mbuf, m);
  489         return n;
  490 }
  491 
  492 static __inline
  493 void
  494 m_clget(struct mbuf *m, int how)
  495 {
  496 
  497         m->m_ext.ext_buf = NULL;
  498         uma_zalloc_arg(zone_clust, m, how);
  499         /*
  500          * On a cluster allocation failure, drain the packet zone and retry,
  501          * we might be able to loosen a few clusters up on the drain.
  502          */
  503         if ((how & M_NOWAIT) && (m->m_ext.ext_buf == NULL)) {
  504                 zone_drain(zone_pack);
  505                 uma_zalloc_arg(zone_clust, m, how);
  506         }
  507 }
  508 
  509 /*
  510  * m_cljget() is different from m_clget() as it can allocate clusters
  511  * without attaching them to an mbuf.  In that case the return value
  512  * is the pointer to the cluster of the requested size.  If an mbuf was
  513  * specified, it gets the cluster attached to it and the return value
  514  * can be safely ignored.
  515  * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES.
  516  */
  517 static __inline
  518 void *
  519 m_cljget(struct mbuf *m, int how, int size)
  520 {
  521         uma_zone_t zone;
  522 
  523         if (m && m->m_flags & M_EXT)
  524                 printf("%s: %p mbuf already has cluster\n", __func__, m);
  525         if (m != NULL)
  526                 m->m_ext.ext_buf = NULL;
  527 
  528         switch (size) {
  529         case MCLBYTES:
  530                 zone = zone_clust;
  531                 break;
  532 #if MJUMPAGESIZE != MCLBYTES
  533         case MJUMPAGESIZE:
  534                 zone = zone_jumbop;
  535                 break;
  536 #endif
  537         case MJUM9BYTES:
  538                 zone = zone_jumbo9;
  539                 break;
  540         case MJUM16BYTES:
  541                 zone = zone_jumbo16;
  542                 break;
  543         default:
  544                 panic("%s: m_getjcl: invalid cluster type", __func__);
  545         }
  546         
  547         return (uma_zalloc_arg(zone, m, how));
  548 }
  549 
  550 static __inline
  551 void
  552 m_chtype(struct mbuf *m, short new_type)
  553 {
  554         m->m_type = new_type;
  555 }
  556 
  557 static __inline struct mbuf *
  558 m_last(struct mbuf *m)
  559 {
  560 
  561         while (m->m_next)
  562                 m = m->m_next;
  563         return (m);
  564 }
  565 
  566 /*
  567  * mbuf, cluster, and external object allocation macros
  568  * (for compatibility purposes).
  569  */
  570 /* NB: M_COPY_PKTHDR is deprecated.  Use M_MOVE_PKTHDR or m_dup_pktdr. */
  571 #define M_MOVE_PKTHDR(to, from) m_move_pkthdr((to), (from))
  572 #define MGET(m, how, type)      ((m) = m_get((how), (type)))
  573 #define MGETHDR(m, how, type)   ((m) = m_gethdr((how), (type)))
  574 #define MCLGET(m, how)          m_clget((m), (how))
  575 #define MEXTADD(m, buf, size, free, args, flags, type)                  \
  576     m_extadd((m), (caddr_t)(buf), (size), (free), (args), (flags), (type))
  577 
  578 /*
  579  * Evaluate TRUE if it's safe to write to the mbuf m's data region (this
  580  * can be both the local data payload, or an external buffer area,
  581  * depending on whether M_EXT is set).
  582  */
  583 #define M_WRITABLE(m)   (!((m)->m_flags & M_RDONLY) && (!((m)->m_flags  \
  584                             & M_EXT) || !MEXT_IS_REF(m)))
  585 
  586 /* Check if the supplied mbuf has a packet header, or else panic. */
  587 #define M_ASSERTPKTHDR(m)                                               \
  588         KASSERT(m != NULL && m->m_flags & M_PKTHDR,                     \
  589             ("%s: no mbuf packet header!", __func__))
  590 
  591 /* Ensure that the supplied mbuf is a valid, non-free mbuf. */
  592 #define M_ASSERTVALID(m)                                                \
  593         KASSERT((((struct mbuf *)m)->m_flags & M_FREELIST) == 0,        \
  594             ("%s: attempted use of a free mbuf!", __func__))
  595 
  596 /*
  597  * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place
  598  * an object of the specified size at the end of the mbuf, longword aligned.
  599  */
  600 #define M_ALIGN(m, len) do {                                            \
  601         (m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1);            \
  602 } while (0)
  603 
  604 /*
  605  * As above, for mbufs allocated with m_gethdr/MGETHDR
  606  * or initialized by M_COPY_PKTHDR.
  607  */
  608 #define MH_ALIGN(m, len) do {                                           \
  609         (m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1);           \
  610 } while (0)
  611 
  612 /*
  613  * Compute the amount of space available
  614  * before the current start of data in an mbuf.
  615  *
  616  * The M_WRITABLE() is a temporary, conservative safety measure: the burden
  617  * of checking writability of the mbuf data area rests solely with the caller.
  618  */
  619 #define M_LEADINGSPACE(m)                                               \
  620         ((m)->m_flags & M_EXT ?                                         \
  621             (M_WRITABLE(m) ? (m)->m_data - (m)->m_ext.ext_buf : 0):     \
  622             (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat :     \
  623             (m)->m_data - (m)->m_dat)
  624 
  625 /*
  626  * Compute the amount of space available
  627  * after the end of data in an mbuf.
  628  *
  629  * The M_WRITABLE() is a temporary, conservative safety measure: the burden
  630  * of checking writability of the mbuf data area rests solely with the caller.
  631  */
  632 #define M_TRAILINGSPACE(m)                                              \
  633         ((m)->m_flags & M_EXT ?                                         \
  634             (M_WRITABLE(m) ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size   \
  635                 - ((m)->m_data + (m)->m_len) : 0) :                     \
  636             &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len))
  637 
  638 /*
  639  * Arrange to prepend space of size plen to mbuf m.
  640  * If a new mbuf must be allocated, how specifies whether to wait.
  641  * If the allocation fails, the original mbuf chain is freed and m is
  642  * set to NULL.
  643  */
  644 #define M_PREPEND(m, plen, how) do {                                    \
  645         struct mbuf **_mmp = &(m);                                      \
  646         struct mbuf *_mm = *_mmp;                                       \
  647         int _mplen = (plen);                                            \
  648         int __mhow = (how);                                             \
  649                                                                         \
  650         MBUF_CHECKSLEEP(how);                                           \
  651         if (M_LEADINGSPACE(_mm) >= _mplen) {                            \
  652                 _mm->m_data -= _mplen;                                  \
  653                 _mm->m_len += _mplen;                                   \
  654         } else                                                          \
  655                 _mm = m_prepend(_mm, _mplen, __mhow);                   \
  656         if (_mm != NULL && _mm->m_flags & M_PKTHDR)                     \
  657                 _mm->m_pkthdr.len += _mplen;                            \
  658         *_mmp = _mm;                                                    \
  659 } while (0)
  660 
  661 /*
  662  * Change mbuf to new type.
  663  * This is a relatively expensive operation and should be avoided.
  664  */
  665 #define MCHTYPE(m, t)   m_chtype((m), (t))
  666 
  667 /* Length to m_copy to copy all. */
  668 #define M_COPYALL       1000000000
  669 
  670 /* Compatibility with 4.3. */
  671 #define m_copy(m, o, l) m_copym((m), (o), (l), M_DONTWAIT)
  672 
  673 extern  int max_datalen;                /* MHLEN - max_hdr */
  674 extern  int max_hdr;                    /* Largest link + protocol header */
  675 extern  int max_linkhdr;                /* Largest link-level header */
  676 extern  int max_protohdr;               /* Largest protocol header */
  677 extern  struct mbstat mbstat;           /* General mbuf stats/infos */
  678 extern  int nmbclusters;                /* Maximum number of clusters */
  679 
  680 struct uio;
  681 
  682 void             m_adj(struct mbuf *, int);
  683 void             m_align(struct mbuf *, int);
  684 int              m_apply(struct mbuf *, int, int,
  685                     int (*)(void *, void *, u_int), void *);
  686 int              m_append(struct mbuf *, int, c_caddr_t);
  687 void             m_cat(struct mbuf *, struct mbuf *);
  688 void             m_extadd(struct mbuf *, caddr_t, u_int,
  689                     void (*)(void *, void *), void *, int, int);
  690 void             m_copyback(struct mbuf *, int, int, c_caddr_t);
  691 void             m_copydata(const struct mbuf *, int, int, caddr_t);
  692 struct  mbuf    *m_copym(struct mbuf *, int, int, int);
  693 struct  mbuf    *m_copypacket(struct mbuf *, int);
  694 void             m_copy_pkthdr(struct mbuf *, struct mbuf *);
  695 struct  mbuf    *m_copyup(struct mbuf *n, int len, int dstoff);
  696 struct  mbuf    *m_defrag(struct mbuf *, int);
  697 struct  mbuf    *m_devget(char *, int, int, struct ifnet *,
  698                     void (*)(char *, caddr_t, u_int));
  699 struct  mbuf    *m_dup(struct mbuf *, int);
  700 int              m_dup_pkthdr(struct mbuf *, struct mbuf *, int);
  701 u_int            m_fixhdr(struct mbuf *);
  702 struct  mbuf    *m_fragment(struct mbuf *, int, int);
  703 void             m_freem(struct mbuf *);
  704 struct  mbuf    *m_getm(struct mbuf *, int, int, short);
  705 struct  mbuf    *m_getptr(struct mbuf *, int, int *);
  706 u_int            m_length(struct mbuf *, struct mbuf **);
  707 void             m_move_pkthdr(struct mbuf *, struct mbuf *);
  708 struct  mbuf    *m_prepend(struct mbuf *, int, int);
  709 void             m_print(const struct mbuf *, int);
  710 struct  mbuf    *m_pulldown(struct mbuf *, int, int, int *);
  711 struct  mbuf    *m_pullup(struct mbuf *, int);
  712 struct  mbuf    *m_split(struct mbuf *, int, int);
  713 struct  mbuf    *m_uiotombuf(struct uio *, int, int, int);
  714 struct  mbuf    *m_unshare(struct mbuf *, int how);
  715 
  716 /*-
  717  * Network packets may have annotations attached by affixing a list
  718  * of "packet tags" to the pkthdr structure.  Packet tags are
  719  * dynamically allocated semi-opaque data structures that have
  720  * a fixed header (struct m_tag) that specifies the size of the
  721  * memory block and a <cookie,type> pair that identifies it.
  722  * The cookie is a 32-bit unique unsigned value used to identify
  723  * a module or ABI.  By convention this value is chose as the
  724  * date+time that the module is created, expressed as the number of
  725  * seconds since the epoch (e.g., using date -u +'%s').  The type value
  726  * is an ABI/module-specific value that identifies a particular annotation
  727  * and is private to the module.  For compatibility with systems
  728  * like OpenBSD that define packet tags w/o an ABI/module cookie,
  729  * the value PACKET_ABI_COMPAT is used to implement m_tag_get and
  730  * m_tag_find compatibility shim functions and several tag types are
  731  * defined below.  Users that do not require compatibility should use
  732  * a private cookie value so that packet tag-related definitions
  733  * can be maintained privately.
  734  *
  735  * Note that the packet tag returned by m_tag_alloc has the default
  736  * memory alignment implemented by malloc.  To reference private data
  737  * one can use a construct like:
  738  *
  739  *      struct m_tag *mtag = m_tag_alloc(...);
  740  *      struct foo *p = (struct foo *)(mtag+1);
  741  *
  742  * if the alignment of struct m_tag is sufficient for referencing members
  743  * of struct foo.  Otherwise it is necessary to embed struct m_tag within
  744  * the private data structure to insure proper alignment; e.g.,
  745  *
  746  *      struct foo {
  747  *              struct m_tag    tag;
  748  *              ...
  749  *      };
  750  *      struct foo *p = (struct foo *) m_tag_alloc(...);
  751  *      struct m_tag *mtag = &p->tag;
  752  */
  753 
  754 /*
  755  * Persistent tags stay with an mbuf until the mbuf is reclaimed.
  756  * Otherwise tags are expected to ``vanish'' when they pass through
  757  * a network interface.  For most interfaces this happens normally
  758  * as the tags are reclaimed when the mbuf is free'd.  However in
  759  * some special cases reclaiming must be done manually.  An example
  760  * is packets that pass through the loopback interface.  Also, one
  761  * must be careful to do this when ``turning around'' packets (e.g.,
  762  * icmp_reflect).
  763  *
  764  * To mark a tag persistent bit-or this flag in when defining the
  765  * tag id.  The tag will then be treated as described above.
  766  */
  767 #define MTAG_PERSISTENT                         0x800
  768 
  769 #define PACKET_TAG_NONE                         0  /* Nadda */
  770 
  771 /* Packet tags for use with PACKET_ABI_COMPAT. */
  772 #define PACKET_TAG_IPSEC_IN_DONE                1  /* IPsec applied, in */
  773 #define PACKET_TAG_IPSEC_OUT_DONE               2  /* IPsec applied, out */
  774 #define PACKET_TAG_IPSEC_IN_CRYPTO_DONE         3  /* NIC IPsec crypto done */
  775 #define PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED      4  /* NIC IPsec crypto req'ed */
  776 #define PACKET_TAG_IPSEC_IN_COULD_DO_CRYPTO     5  /* NIC notifies IPsec */
  777 #define PACKET_TAG_IPSEC_PENDING_TDB            6  /* Reminder to do IPsec */
  778 #define PACKET_TAG_BRIDGE                       7  /* Bridge processing done */
  779 #define PACKET_TAG_GIF                          8  /* GIF processing done */
  780 #define PACKET_TAG_GRE                          9  /* GRE processing done */
  781 #define PACKET_TAG_IN_PACKET_CHECKSUM           10 /* NIC checksumming done */
  782 #define PACKET_TAG_ENCAP                        11 /* Encap.  processing */
  783 #define PACKET_TAG_IPSEC_SOCKET                 12 /* IPSEC socket ref */
  784 #define PACKET_TAG_IPSEC_HISTORY                13 /* IPSEC history */
  785 #define PACKET_TAG_IPV6_INPUT                   14 /* IPV6 input processing */
  786 #define PACKET_TAG_DUMMYNET                     15 /* dummynet info */
  787 #define PACKET_TAG_DIVERT                       17 /* divert info */
  788 #define PACKET_TAG_IPFORWARD                    18 /* ipforward info */
  789 #define PACKET_TAG_MACLABEL     (19 | MTAG_PERSISTENT) /* MAC label */
  790 #define PACKET_TAG_PF_ROUTED                    21 /* PF routed, avoid loops */
  791 #define PACKET_TAG_PF_FRAGCACHE                 22 /* PF fragment cached */
  792 #define PACKET_TAG_PF_QID                       23 /* PF ALTQ queue id */
  793 #define PACKET_TAG_PF_TAG                       24 /* PF tagged */
  794 #define PACKET_TAG_RTSOCKFAM                    25 /* rtsock sa family */
  795 #define PACKET_TAG_PF_TRANSLATE_LOCALHOST       26 /* PF translate localhost */
  796 #define PACKET_TAG_IPOPTIONS                    27 /* Saved IP options */
  797 #define PACKET_TAG_CARP                         28 /* CARP info */
  798 
  799 /* Packet tag routines. */
  800 struct  m_tag   *m_tag_alloc(u_int32_t, int, int, int);
  801 void             m_tag_delete(struct mbuf *, struct m_tag *);
  802 void             m_tag_delete_chain(struct mbuf *, struct m_tag *);
  803 void             m_tag_free_default(struct m_tag *);
  804 struct  m_tag   *m_tag_locate(struct mbuf *, u_int32_t, int, struct m_tag *);
  805 struct  m_tag   *m_tag_copy(struct m_tag *, int);
  806 int              m_tag_copy_chain(struct mbuf *, struct mbuf *, int);
  807 void             m_tag_delete_nonpersistent(struct mbuf *);
  808 
  809 /*
  810  * Initialize the list of tags associated with an mbuf.
  811  */
  812 static __inline void
  813 m_tag_init(struct mbuf *m)
  814 {
  815         SLIST_INIT(&m->m_pkthdr.tags);
  816 }
  817 
  818 /*
  819  * Set up the contents of a tag.  Note that this does not
  820  * fill in the free method; the caller is expected to do that.
  821  *
  822  * XXX probably should be called m_tag_init, but that was
  823  * already taken.
  824  */
  825 static __inline void
  826 m_tag_setup(struct m_tag *t, u_int32_t cookie, int type, int len)
  827 {
  828         t->m_tag_id = type;
  829         t->m_tag_len = len;
  830         t->m_tag_cookie = cookie;
  831 }
  832 
  833 /*
  834  * Reclaim resources associated with a tag.
  835  */
  836 static __inline void
  837 m_tag_free(struct m_tag *t)
  838 {
  839         (*t->m_tag_free)(t);
  840 }
  841 
  842 /*
  843  * Return the first tag associated with an mbuf.
  844  */
  845 static __inline struct m_tag *
  846 m_tag_first(struct mbuf *m)
  847 {
  848         return (SLIST_FIRST(&m->m_pkthdr.tags));
  849 }
  850 
  851 /*
  852  * Return the next tag in the list of tags associated with an mbuf.
  853  */
  854 static __inline struct m_tag *
  855 m_tag_next(struct mbuf *m, struct m_tag *t)
  856 {
  857         return (SLIST_NEXT(t, m_tag_link));
  858 }
  859 
  860 /*
  861  * Prepend a tag to the list of tags associated with an mbuf.
  862  */
  863 static __inline void
  864 m_tag_prepend(struct mbuf *m, struct m_tag *t)
  865 {
  866         SLIST_INSERT_HEAD(&m->m_pkthdr.tags, t, m_tag_link);
  867 }
  868 
  869 /*
  870  * Unlink a tag from the list of tags associated with an mbuf.
  871  */
  872 static __inline void
  873 m_tag_unlink(struct mbuf *m, struct m_tag *t)
  874 {
  875         SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link);
  876 }
  877 
  878 /* These are for OpenBSD compatibility. */
  879 #define MTAG_ABI_COMPAT         0               /* compatibility ABI */
  880 
  881 static __inline struct m_tag *
  882 m_tag_get(int type, int length, int wait)
  883 {
  884         return (m_tag_alloc(MTAG_ABI_COMPAT, type, length, wait));
  885 }
  886 
  887 static __inline struct m_tag *
  888 m_tag_find(struct mbuf *m, int type, struct m_tag *start)
  889 {
  890         return (SLIST_EMPTY(&m->m_pkthdr.tags) ?
  891             NULL : m_tag_locate(m, MTAG_ABI_COMPAT, type, start));
  892 }
  893 
  894 #endif /* _KERNEL */
  895 
  896 #endif /* !_SYS_MBUF_H_ */

Cache object: 13d4c48e6647c014bab5a881c45c171b


[ 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.