|
|
1.1 ! root 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 are permitted ! 6: * provided that this notice is preserved and that due credit is given ! 7: * to the University of California at Berkeley. The name of the University ! 8: * may not be used to endorse or promote products derived from this ! 9: * software without specific prior written permission. This software ! 10: * is provided ``as is'' without express or implied warranty. ! 11: * ! 12: * @(#)mbuf.h 7.10 (Berkeley) 2/8/88 ! 13: */ ! 14: ! 15: /* ! 16: * Constants related to memory allocator. ! 17: */ ! 18: #define MSIZE 128 /* size of an mbuf */ ! 19: ! 20: #if CLBYTES > 1024 ! 21: #define MCLBYTES 1024 ! 22: #define MCLSHIFT 10 ! 23: #define MCLOFSET (MCLBYTES - 1) ! 24: #else ! 25: #define MCLBYTES CLBYTES ! 26: #define MCLSHIFT CLSHIFT ! 27: #define MCLOFSET CLOFSET ! 28: #endif ! 29: ! 30: #define MMINOFF 12 /* mbuf header length */ ! 31: #define MTAIL 4 ! 32: #define MMAXOFF (MSIZE-MTAIL) /* offset where data ends */ ! 33: #define MLEN (MSIZE-MMINOFF-MTAIL) /* mbuf data length */ ! 34: #ifdef GATEWAY ! 35: #define NMBCLUSTERS 512 ! 36: #else ! 37: #define NMBCLUSTERS 256 ! 38: #endif ! 39: #define NMBPCL (CLBYTES/MSIZE) /* # mbufs per cluster */ ! 40: ! 41: /* ! 42: * Macros for type conversion ! 43: */ ! 44: ! 45: /* network cluster number to virtual address, and back */ ! 46: #define cltom(x) ((struct mbuf *)((int)mbutl + ((x) << MCLSHIFT))) ! 47: #define mtocl(x) (((int)x - (int)mbutl) >> MCLSHIFT) ! 48: ! 49: /* address in mbuf to mbuf head */ ! 50: #define dtom(x) ((struct mbuf *)((int)x & ~(MSIZE-1))) ! 51: ! 52: /* mbuf head, to typed data */ ! 53: #define mtod(x,t) ((t)((int)(x) + (x)->m_off)) ! 54: ! 55: struct mbuf { ! 56: struct mbuf *m_next; /* next buffer in chain */ ! 57: u_long m_off; /* offset of data */ ! 58: short m_len; /* amount of data in this mbuf */ ! 59: short m_type; /* mbuf type (0 == free) */ ! 60: u_char m_dat[MLEN]; /* data storage */ ! 61: struct mbuf *m_act; /* link in higher-level mbuf list */ ! 62: }; ! 63: ! 64: /* mbuf types */ ! 65: #define MT_FREE 0 /* should be on free list */ ! 66: #define MT_DATA 1 /* dynamic (data) allocation */ ! 67: #define MT_HEADER 2 /* packet header */ ! 68: #define MT_SOCKET 3 /* socket structure */ ! 69: #define MT_PCB 4 /* protocol control block */ ! 70: #define MT_RTABLE 5 /* routing tables */ ! 71: #define MT_HTABLE 6 /* IMP host tables */ ! 72: #define MT_ATABLE 7 /* address resolution tables */ ! 73: #define MT_SONAME 8 /* socket name */ ! 74: #define MT_SOOPTS 10 /* socket options */ ! 75: #define MT_FTABLE 11 /* fragment reassembly header */ ! 76: #define MT_RIGHTS 12 /* access rights */ ! 77: #define MT_IFADDR 13 /* interface address */ ! 78: ! 79: /* flags to m_get */ ! 80: #define M_DONTWAIT 0 ! 81: #define M_WAIT 1 ! 82: ! 83: /* flags to m_pgalloc */ ! 84: #define MPG_MBUFS 0 /* put new mbufs on free list */ ! 85: #define MPG_CLUSTERS 1 /* put new clusters on free list */ ! 86: ! 87: /* length to m_copy to copy all */ ! 88: #define M_COPYALL 1000000000 ! 89: ! 90: /* ! 91: * m_pullup will pull up additional length if convenient; ! 92: * should be enough to hold headers of second-level and higher protocols. ! 93: */ ! 94: #define MPULL_EXTRA 32 ! 95: ! 96: #define MGET(m, i, t) \ ! 97: { int ms = splimp(); \ ! 98: if ((m)=mfree) \ ! 99: { if ((m)->m_type != MT_FREE) panic("mget"); (m)->m_type = t; \ ! 100: mbstat.m_mtypes[MT_FREE]--; mbstat.m_mtypes[t]++; \ ! 101: mfree = (m)->m_next; (m)->m_next = 0; \ ! 102: (m)->m_off = MMINOFF; } \ ! 103: else \ ! 104: (m) = m_more(i, t); \ ! 105: splx(ms); } ! 106: /* ! 107: * Mbuf page cluster macros. ! 108: * MCLALLOC allocates mbuf page clusters. ! 109: * Note that it works only with a count of 1 at the moment. ! 110: * MCLGET adds such clusters to a normal mbuf. ! 111: * m->m_len is set to MCLBYTES upon success, and to MLEN on failure. ! 112: * MCLFREE frees clusters allocated by MCLALLOC. ! 113: */ ! 114: #define MCLALLOC(m, i) \ ! 115: { int ms = splimp(); \ ! 116: if (mclfree == 0) \ ! 117: (void)m_clalloc((i), MPG_CLUSTERS, M_DONTWAIT); \ ! 118: if ((m)=mclfree) \ ! 119: {++mclrefcnt[mtocl(m)];mbstat.m_clfree--;mclfree = (m)->m_next;} \ ! 120: splx(ms); } ! 121: #define M_HASCL(m) ((m)->m_off >= MSIZE) ! 122: #define MTOCL(m) ((struct mbuf *)(mtod((m), int) &~ MCLOFSET)) ! 123: ! 124: #define MCLGET(m) \ ! 125: { struct mbuf *p; \ ! 126: MCLALLOC(p, 1); \ ! 127: if (p) { \ ! 128: (m)->m_off = (int)p - (int)(m); \ ! 129: (m)->m_len = MCLBYTES; \ ! 130: } else \ ! 131: (m)->m_len = MLEN; \ ! 132: } ! 133: #define MCLFREE(m) { \ ! 134: if (--mclrefcnt[mtocl(m)] == 0) \ ! 135: { (m)->m_next = mclfree;mclfree = (m);mbstat.m_clfree++;} \ ! 136: } ! 137: #define MFREE(m, n) \ ! 138: { int ms = splimp(); \ ! 139: if ((m)->m_type == MT_FREE) panic("mfree"); \ ! 140: mbstat.m_mtypes[(m)->m_type]--; mbstat.m_mtypes[MT_FREE]++; \ ! 141: (m)->m_type = MT_FREE; \ ! 142: if (M_HASCL(m)) { \ ! 143: (n) = MTOCL(m); \ ! 144: MCLFREE(n); \ ! 145: } \ ! 146: (n) = (m)->m_next; (m)->m_next = mfree; \ ! 147: (m)->m_off = 0; (m)->m_act = 0; mfree = (m); \ ! 148: splx(ms); \ ! 149: if (m_want) { \ ! 150: m_want = 0; \ ! 151: wakeup((caddr_t)&mfree); \ ! 152: } \ ! 153: } ! 154: ! 155: /* ! 156: * Mbuf statistics. ! 157: */ ! 158: struct mbstat { ! 159: u_long m_mbufs; /* mbufs obtained from page pool */ ! 160: u_long m_clusters; /* clusters obtained from page pool */ ! 161: u_long m_spare; /* spare field */ ! 162: u_long m_clfree; /* free clusters */ ! 163: u_long m_drops; /* times failed to find space */ ! 164: u_long m_wait; /* times waited for space */ ! 165: u_long m_drain; /* times drained protocols for space */ ! 166: u_short m_mtypes[256]; /* type specific mbuf allocations */ ! 167: }; ! 168: ! 169: #ifdef KERNEL ! 170: extern struct mbuf mbutl[]; /* virtual address of net free mem */ ! 171: extern struct pte Mbmap[]; /* page tables to map Netutl */ ! 172: struct mbstat mbstat; ! 173: int nmbclusters; ! 174: struct mbuf *mfree, *mclfree; ! 175: char mclrefcnt[NMBCLUSTERS + 1]; ! 176: int m_want; ! 177: struct mbuf *m_get(),*m_getclr(),*m_free(),*m_more(),*m_copy(),*m_pullup(); ! 178: caddr_t m_clalloc(); ! 179: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.