home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / gnu / ixemul-39.47-env-bin.lha / include / sys / mbuf.h < prev    next >
C/C++ Source or Header  |  1994-02-23  |  12KB  |  377 lines

  1. /*
  2.  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
  3.  * 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. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  *
  33.  *    @(#)mbuf.h    7.14 (Berkeley) 12/5/90
  34.  */
  35.  
  36. #ifndef M_WAITOK
  37. #include "malloc.h"
  38. #endif
  39.  
  40. /*
  41.  * Mbufs are of a single size, MSIZE (machine/machparam.h), which
  42.  * includes overhead.  An mbuf may add a single "mbuf cluster" of size
  43.  * MCLBYTES (also in machine/machparam.h), which has no additional overhead
  44.  * and is used instead of the internal data area; this is done when
  45.  * at least MINCLSIZE of data must be stored.
  46.  */
  47.  
  48. #define    MLEN        (MSIZE - sizeof(struct m_hdr))    /* normal data len */
  49. #define    MHLEN        (MLEN - sizeof(struct pkthdr))    /* data len w/pkthdr */
  50.  
  51. #define    MINCLSIZE    (MHLEN + MLEN)    /* smallest amount to put in cluster */
  52. #define    M_MAXCOMPRESS    (MHLEN / 2)    /* max amount to copy for compression */
  53.  
  54. /*
  55.  * Macros for type conversion
  56.  * mtod(m,t) -    convert mbuf pointer to data pointer of correct type
  57.  * dtom(x) -    convert data pointer within mbuf to mbuf pointer (XXX)
  58.  */
  59. #define mtod(m,t)    ((t)((m)->m_data))
  60. #define    dtom(x)        ((struct mbuf *)((int)(x) & ~(MSIZE-1)))
  61.  
  62. /* header at beginning of each mbuf: */
  63. struct m_hdr {
  64.     struct    mbuf *mh_next;        /* next buffer in chain */
  65.     struct    mbuf *mh_nextpkt;    /* next chain in queue/record */
  66.     int    mh_len;            /* amount of data in this mbuf */
  67.     caddr_t    mh_data;        /* location of data */
  68.     short    mh_type;        /* type of data in this mbuf */
  69.     short    mh_flags;        /* flags; see below */
  70. };
  71.  
  72. /* record/packet header in first mbuf of chain; valid if M_PKTHDR set */
  73. struct    pkthdr {
  74.     int    len;        /* total packet length */
  75.     struct    ifnet *rcvif;    /* rcv interface */
  76. };
  77.  
  78. /* description of external storage mapped into mbuf, valid if M_EXT set */
  79. struct m_ext {
  80.     caddr_t    ext_buf;        /* start of buffer */
  81.     void    (*ext_free)();        /* free routine if not the usual */
  82.     u_int    ext_size;        /* size of buffer, for ext_free */
  83. };
  84.  
  85. struct mbuf {
  86.     struct    m_hdr m_hdr;
  87.     union {
  88.         struct {
  89.             struct    pkthdr MH_pkthdr;    /* M_PKTHDR set */
  90.             union {
  91.                 struct    m_ext MH_ext;    /* M_EXT set */
  92.                 char    MH_databuf[MHLEN];
  93.             } MH_dat;
  94.         } MH;
  95.         char    M_databuf[MLEN];        /* !M_PKTHDR, !M_EXT */
  96.     } M_dat;
  97. };
  98. #define    m_next        m_hdr.mh_next
  99. #define    m_len        m_hdr.mh_len
  100. #define    m_data        m_hdr.mh_data
  101. #define    m_type        m_hdr.mh_type
  102. #define    m_flags        m_hdr.mh_flags
  103. #define    m_nextpkt    m_hdr.mh_nextpkt
  104. #define    m_act        m_nextpkt
  105. #define    m_pkthdr    M_dat.MH.MH_pkthdr
  106. #define    m_ext        M_dat.MH.MH_dat.MH_ext
  107. #define    m_pktdat    M_dat.MH.MH_dat.MH_databuf
  108. #define    m_dat        M_dat.M_databuf
  109.  
  110. /* mbuf flags */
  111. #define    M_EXT        0x0001    /* has associated external storage */
  112. #define    M_PKTHDR    0x0002    /* start of record */
  113. #define    M_EOR        0x0004    /* end of record */
  114.  
  115. /* mbuf pkthdr flags, also in m_flags */
  116. #define    M_BCAST        0x0100    /* send/received as link-level broadcast */
  117. #define    M_MCAST        0x0200    /* send/received as link-level multicast */
  118.  
  119. /* flags copied when copying m_pkthdr */
  120. #define    M_COPYFLAGS    (M_PKTHDR|M_EOR|M_BCAST|M_MCAST)
  121.  
  122. /* mbuf types */
  123. #define    MT_FREE        0    /* should be on free list */
  124. #define    MT_DATA        1    /* dynamic (data) allocation */
  125. #define    MT_HEADER    2    /* packet header */
  126. #define    MT_SOCKET    3    /* socket structure */
  127. #define    MT_PCB        4    /* protocol control block */
  128. #define    MT_RTABLE    5    /* routing tables */
  129. #define    MT_HTABLE    6    /* IMP host tables */
  130. #define    MT_ATABLE    7    /* address resolution tables */
  131. #define    MT_SONAME    8    /* socket name */
  132. #define    MT_SOOPTS    10    /* socket options */
  133. #define    MT_FTABLE    11    /* fragment reassembly header */
  134. #define    MT_RIGHTS    12    /* access rights */
  135. #define    MT_IFADDR    13    /* interface address */
  136. #define MT_CONTROL    14    /* extra-data protocol message */
  137. #define MT_OOBDATA    15    /* expedited data  */
  138.  
  139. /* flags to m_get/MGET */
  140. #define    M_DONTWAIT    M_NOWAIT
  141. #define    M_WAIT        M_WAITOK
  142.  
  143. /*
  144.  * mbuf allocation/deallocation macros:
  145.  *
  146.  *    MGET(struct mbuf *m, int how, int type)
  147.  * allocates an mbuf and initializes it to contain internal data.
  148.  *
  149.  *    MGETHDR(struct mbuf *m, int how, int type)
  150.  * allocates an mbuf and initializes it to contain a packet header
  151.  * and internal data.
  152.  */
  153. #define    MGET(m, how, type) { \
  154.     (m) = (struct mbuf *) mb_alloc (); \
  155.     if (m) { \
  156.         (m)->m_type = (type); \
  157.         mbstat.m_mtypes[type]++; \
  158.         (m)->m_next = (struct mbuf *)NULL; \
  159.         (m)->m_nextpkt = (struct mbuf *)NULL; \
  160.         (m)->m_data = (m)->m_dat; \
  161.         (m)->m_flags = 0; \
  162.     } else \
  163.         (m) = m_retry((how), (type)); \
  164. }
  165.  
  166. #define    MGETHDR(m, how, type) { \
  167.     (m) = (struct mbuf *) mb_alloc (); \
  168.     if (m) { \
  169.         (m)->m_type = (type); \
  170.         mbstat.m_mtypes[type]++; \
  171.         (m)->m_next = (struct mbuf *)NULL; \
  172.         (m)->m_nextpkt = (struct mbuf *)NULL; \
  173.         (m)->m_data = (m)->m_pktdat; \
  174.         (m)->m_flags = M_PKTHDR; \
  175.     } else \
  176.         (m) = m_retryhdr((how), (type)); \
  177. }
  178.  
  179. /*
  180.  * Mbuf cluster macros.
  181.  * MCLALLOC(caddr_t p, int how) allocates an mbuf cluster.
  182.  * MCLGET adds such clusters to a normal mbuf;
  183.  * the flag M_EXT is set upon success.
  184.  * MCLFREE releases a reference to a cluster allocated by MCLALLOC,
  185.  * freeing the cluster if the reference count has reached 0.
  186.  *
  187.  * Normal mbuf clusters are normally treated as character arrays
  188.  * after allocation, but use the first word of the buffer as a free list
  189.  * pointer while on the free list.
  190.  */
  191. union mcluster {
  192.     union    mcluster *mcl_next;
  193.     char    mcl_buf[MCLBYTES];
  194. };
  195.  
  196. #define    MCLALLOC(p, how) \
  197.     { int ms = splimp(); \
  198.       if (mclfree == 0) \
  199.         (void)m_clalloc(1, (how)); \
  200.       if ((p) = (caddr_t)mclfree) { \
  201.           mcl_op (p, +1); \
  202.         mbstat.m_clfree--; \
  203.         mclfree = ((union mcluster *)(p))->mcl_next; \
  204.       } \
  205.       splx(ms); \
  206.     }
  207.  
  208. #define    MCLGET(m, how) \
  209.     { MCLALLOC((m)->m_ext.ext_buf, (how)); \
  210.       if ((m)->m_ext.ext_buf != NULL) { \
  211.         (m)->m_data = (m)->m_ext.ext_buf; \
  212.         (m)->m_flags |= M_EXT; \
  213.         (m)->m_ext.ext_size = MCLBYTES;  \
  214.       } \
  215.     }
  216.  
  217. #define    MCLFREE(p) \
  218.     { int ms = splimp(); \
  219.       if (mcl_op (p, -1) == 0) { \
  220.         ((union mcluster *)(p))->mcl_next = mclfree; \
  221.         mclfree = (union mcluster *)(p); \
  222.         mbstat.m_clfree++; \
  223.       } \
  224.       splx(ms); \
  225.     }
  226.  
  227. /*
  228.  * MFREE(struct mbuf *m, struct mbuf *n)
  229.  * Free a single mbuf and associated external storage.
  230.  * Place the successor, if any, in n.
  231.  */
  232. #ifdef notyet
  233. #define    MFREE(m, n) \
  234.     { mbstat.m_mtypes[(m)->m_type]--; \
  235.       if ((m)->m_flags & M_EXT) { \
  236.         if ((m)->m_ext.ext_free) \
  237.             (*((m)->m_ext.ext_free))((m)->m_ext.ext_buf, \
  238.                 (m)->m_ext.ext_size); \
  239.         else \
  240.             MCLFREE((m)->m_ext.ext_buf); \
  241.       } \
  242.       (n) = (m)->m_next; \
  243.       mb_free (m); \
  244.     }
  245. #else /* notyet */
  246. #define    MFREE(m, nn) \
  247.     { mbstat.m_mtypes[(m)->m_type]--; \
  248.       if ((m)->m_flags & M_EXT) { \
  249.         MCLFREE((m)-