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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

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

Cache object: 21a233cfc52ed7dc2004e68d2b39c6fd


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