home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.2 (Developer) / NS_dev_3.2.iso / NextDeveloper / Headers / bsd / sys / mbuf.h < prev    next >
C/C++ Source or Header  |  1993-10-19  |  8KB  |  251 lines

  1. /* 
  2.  * Mach Operating System
  3.  * Copyright (c) 1987 Carnegie-Mellon University
  4.  * All rights reserved.  The CMU software License Agreement specifies
  5.  * the terms and conditions for use and redistribution.
  6.  */
  7. /*
  8.  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
  9.  * All rights reserved.
  10.  *
  11.  * Redistribution and use in source and binary forms are permitted
  12.  * provided that this notice is preserved and that due credit is given
  13.  * to the University of California at Berkeley. The name of the University
  14.  * may not be used to endorse or promote products derived from this
  15.  * software without specific prior written permission. This software
  16.  * is provided ``as is'' without express or implied warranty.
  17.  **********************************************************************
  18.  * HISTORY
  19.  * 27-Sep-89  Morris Meyer (mmeyer) at NeXT
  20.  *    NFS 4.0 Changes.
  21.  *
  22.  * 11-Aug-87  Peter King (king) at NeXT
  23.  *    SUN_RPC: Added support for "funny" mbuf clusters.  MFREE calls
  24.  *         mclput() rather than MCLFREE.
  25.  *
  26.  * 18-Nov-86  David Golub (dbg) at Carnegie-Mellon University
  27.  *    Remove all uses of 'pte' from machine-independent code.
  28.  *
  29.  *    @(#)mbuf.h    7.8.1.2 (Berkeley) 2/8/88
  30.  */
  31.  
  32. /*
  33.  * Constants related to memory allocator.
  34.  */
  35. #define    MSIZE        128            /* size of an mbuf */
  36.  
  37. /* CLBYTES is not a constant for the NeXT */
  38. #if CLBYTES > 1024 || defined(NeXT)
  39. #define    MCLBYTES    1024
  40. #define    MCLSHIFT    10
  41. #define    MCLOFSET    (MCLBYTES - 1)
  42. #else
  43. #define    MCLBYTES    CLBYTES
  44. #define    MCLSHIFT    CLSHIFT
  45. #define    MCLOFSET    CLOFSET
  46. #endif
  47.  
  48. #define    MMINOFF        12            /* mbuf header length */
  49. #define    MTAIL        4
  50. #define    MMAXOFF        (MSIZE-MTAIL)        /* offset where data ends */
  51. #define    MLEN        (MSIZE-MMINOFF-MTAIL)    /* mbuf data length */
  52. #if defined(GATEWAY) || defined(NeXT)
  53. #define    NMBCLUSTERS    512
  54. #else
  55. #define    NMBCLUSTERS    256
  56. #endif
  57. #define    NMBPCL        (CLBYTES/MSIZE)        /* # mbufs per cluster */
  58.  
  59. /*
  60.  * Macros for type conversion
  61.  */
  62.  
  63. /* network cluster number to virtual address, and back */
  64. #define    cltom(x) ((struct mbuf *)((int)mbutl + ((x) << MCLSHIFT)))
  65. #define    mtocl(x) (((int)x - (int)mbutl) >> MCLSHIFT)
  66.  
  67. /* address in mbuf to mbuf head */
  68. #define    dtom(x)        ((struct mbuf *)((int)x & ~(MSIZE-1)))
  69.  
  70. /* mbuf head, to typed data */
  71. #define    mtod(x,t)    ((t)((int)(x) + (x)->m_off))
  72.  
  73. /*
  74.  * Standard 4.3 supports two kinds of mbufs: normal mbufs, with data
  75.  * residing in the mbuf itself; and cluster mbufs with data stored
  76.  * elsewhere (at a higher address than the corresponding mbuf).
  77.  * The data area for cluster mbufs is allocated as a pool and is
  78.  * managed by the mbuf system itself.  We add another kind of cluster
  79.  * mbuf, whose data area is not part of the mbuf system.  Instead,
  80.  * a routine wishing to use this kind of mbuf must provide a buffer
  81.  * for the storage associated with the mbuf and a pointer to a routine
  82.  * to free the buffer when the mbuf is freed.  The fields in mun_cl
  83.  * below record the necessary bookkeeping information.
  84.  */
  85. struct mbuf {
  86.     struct    mbuf *m_next;        /* next buffer in chain */
  87.     u_long    m_off;            /* offset of data */
  88.     short    m_len;            /* amount of data in this mbuf */
  89.     short    m_type;            /* mbuf type (0 == free) */
  90.     union {
  91.         u_char    mun_dat[MLEN];    /* data storage */
  92.         struct {
  93.             short    mun_cltype;    /* "cluster" type */
  94.             int    (*mun_clfun)();
  95.             int    mun_clarg;
  96.             int    (*mun_clswp)();
  97.         } mun_cl;
  98.     } m_un;
  99.     struct    mbuf *m_act;        /* link in higher-level mbuf list */
  100. };
  101. #define    m_dat    m_un.mun_dat
  102. #define    m_cltype m_un.mun_cl.mun_cltype
  103. #define    m_clfun    m_un.mun_cl.mun_clfun
  104. #define    m_clarg    m_un.mun_cl.mun_clarg
  105. #define    m_clswp    m_un.mun_cl.mun_clswp
  106.  
  107. /* mbuf types */
  108. #define    MT_FREE        0    /* should be on free list */
  109. #define    MT_DATA        1    /* dynamic (data) allocation */
  110. #define    MT_HEADER    2    /* packet header */
  111. #define    MT_SOCKET    3    /* socket structure */
  112. #define    MT_PCB        4    /* protocol control block */
  113. #define    MT_RTABLE    5    /* routing tables */
  114. #define    MT_HTABLE    6    /* IMP host tables */
  115. #define    MT_ATABLE    7    /* address resolution tables */
  116. #define    MT_SONAME    8    /* socket name */
  117. #define    MT_ZOMBIE    9    /* zombie proc status */
  118. #define    MT_SOOPTS    10    /* socket options */
  119. #define    MT_FTABLE    11    /* fragment reassembly header */
  120. #define    MT_RIGHTS    12    /* access rights */
  121. #define    MT_IFADDR    13    /* interface address */
  122. #if    NeXT
  123. #define    MT_MAX        32
  124. #endif    NeXT
  125.  
  126. /*
  127.  * Values for m_cltype: applicable only for cluster mbufs
  128.  */
  129. #define    MCL_STATIC    1    /* data in mbuf cluster pool */
  130. #define    MCL_LOANED    2    /* data allocated elsewhere and "loaned"
  131.                    to the mbuf */
  132.  
  133. /* flags to m_get */
  134. #define    M_DONTWAIT    0
  135. #define    M_WAIT        1
  136.  
  137. /* flags to m_pgalloc */
  138. #define    MPG_MBUFS    0        /* put new mbufs on free list */
  139. #define    MPG_CLUSTERS    1        /* put new clusters on free list */
  140. #define    MPG_SPACE    2        /* don't free; caller wants space */
  141.  
  142. /* length to m_copy to copy all */
  143. #define    M_COPYALL    1000000000
  144.  
  145. /*
  146.  * m_pullup will pull up additional length if convenient;
  147.  * should be enough to hold headers of second-level and higher protocols. 
  148.  */
  149. #define    MPULL_EXTRA    32
  150.  
  151. /*
  152.  * Macros for mbuf manipulation.
  153.  *
  154.  * Standard 4.3 defines some additional macros not given here
  155.  * (MTOCL, MCLFREE), as they are useful only within uipc_mbuf.c
  156.  * or can't cope with the existence of the MCL_LOANED cluster
  157.  * mbuf type.
  158.  *
  159.  * MCLALLOC allocates MCL_STATIC clusters.  Note that it works
  160.  * only with a count of 1 at the moment.  MCLGET connects such clusters
  161.  * to a normal mbuf.  m->m_len is set to CLBYTES upon success.
  162.  */
  163. #define    MGET(m, i, t) \
  164.     { int ms = splimp(); \
  165.       if ((m)=mfree) \
  166.         { if ((m)->m_type != MT_FREE) panic("mget"); (m)->m_type = t; \
  167.           mbstat.m_mtypes[MT_FREE]--; mbstat.m_mtypes[t]++; \
  168.           mfree = (m)->m_next; (m)->m_next = 0; \
  169.           (m)->m_off = MMINOFF; } \
  170.       else \
  171.         (m) = m_more(i, t); \
  172.       splx(ms); }
  173. /*
  174.  * Mbuf page cluster macros.
  175.  * MCLALLOC allocates mbuf page clusters.
  176.  * Note that it works only with a count of 1 at the moment.
  177.  * MCLGET adds such clusters to a normal mbuf.
  178.  * m->m_len is set to MCLBYTES upon success, and to MLEN on failure.
  179.  * MCLFREE frees clusters allocated by MCLALLOC.
  180.  */
  181. #define    MCLALLOC(m, i) \
  182.     { int ms = splimp(); \
  183.       if (mclfree == 0) \
  184.         (void)m_clalloc((i), MPG_CLUSTERS, M_DONTWAIT); \
  185.       if ((m)=mclfree) \
  186.          {++mclrefcnt[mtocl(m)];mbstat.m_clfree--;mclfree = (m)->m_next;} \
  187.       splx(ms); }
  188. #define    M_HASCL(m)    ((m)->m_off >= MSIZE)
  189. #define    MTOCL(m)    ((struct mbuf *)(mtod((m), int) &~ MCLOFSET))
  190.  
  191. #define    MCLGET(m) \
  192.     { struct mbuf *p; \
  193.       MCLALLOC(p, 1); \
  194.       if (p) { \
  195.         (m)->m_off = (int)p - (int)(m); \
  196.         (m)->m_len = MCLBYTES; \
  197.         (m)->m_cltype = MCL_STATIC; \
  198.       } else \
  199.         (m)->m_len = MLEN; \
  200.     }
  201. #define    MCLFREE(m) { \
  202.     if (--mclrefcnt[mtocl(m)] == 0) \
  203.         { (m)->m_next = mclfree;mclfree = (m);mbstat.m_clfree++;} \
  204.     }
  205. #define    MFREE(m, n) \
  206.     { int ms = splimp(); \
  207.       if ((m)->m_type == MT_FREE) panic("mfree"); \
  208.       mbstat.m_mtypes[(m)->m_type]--; mbstat.m_mtypes[MT_FREE]++; \
  209.       (m)->m_type = MT_FREE; \
  210.       if (M_HASCL(m)) { \
  211.           mclput(m); \
  212.       } \
  213.       (n) = (m)->m_next; (m)->m_next = mfree; \
  214.       (m)->m_off = 0; (m)->m_act = 0; mfree = (m); \
  215.       splx(ms); \
  216.       if (m_want) { \
  217.           m_want = 0; \
  218.           wakeup((caddr_t)&mfree); \
  219.       } \
  220.     }
  221.  
  222. /*
  223.  * Mbuf statistics.
  224.  */
  225. struct mbstat {
  226.     u_long    m_mbufs;    /* mbufs obtained from page pool */
  227.     u_long    m_clusters;    /* clusters obtained from page pool */
  228.     u_long    m_space;    /* interface pages obtained from page pool */
  229.     u_long    m_clfree;    /* free clusters */
  230.     u_long    m_drops;    /* times failed to find space */
  231.     u_long    m_wait;        /* times waited for space */
  232.     u_long    m_drain;    /* times drained protocols for space */
  233. #if    NeXT
  234.     u_short    m_mtypes[MT_MAX];    /* type specific mbuf allocations */
  235. #else    NeXT
  236.     u_short    m_mtypes[256];    /* type specific mbuf allocations */
  237. #endif    NeXT
  238. };
  239.  
  240. #ifdef    KERNEL
  241. struct mbuf *mbutl, embutl;    /* virtual address of net free mem */
  242. struct    mbstat mbstat;
  243. int    nmbclusters;
  244. struct    mbuf *mfree, *mclfree;
  245. char    mclrefcnt[NMBCLUSTERS + 1];
  246. int    m_want;
  247. struct    mbuf *m_get(),*m_getclr(),*m_free(),*m_more(),*m_copy(),*m_pullup();
  248. struct    mbuf *mclgetx();
  249. caddr_t    m_clalloc();
  250. #endif
  251.