bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 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. |
aliguori | 2f5f899 | 2009-01-26 19:37:41 +0000 | [diff] [blame] | 13 | * 3. Neither the name of the University nor the names of its contributors |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 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.3 (Berkeley) 1/21/94 |
| 30 | * mbuf.h,v 1.9 1994/11/14 13:54:20 bde Exp |
| 31 | */ |
| 32 | |
| 33 | #ifndef _MBUF_H_ |
| 34 | #define _MBUF_H_ |
| 35 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 36 | #define MINCSIZE 4096 /* Amount to increase mbuf if too small */ |
| 37 | |
| 38 | /* |
| 39 | * Macros for type conversion |
| 40 | * mtod(m,t) - convert mbuf pointer to data pointer of correct type |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 41 | */ |
| 42 | #define mtod(m,t) ((t)(m)->m_data) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 43 | |
| 44 | /* XXX About mbufs for slirp: |
| 45 | * Only one mbuf is ever used in a chain, for each "cell" of data. |
| 46 | * m_nextpkt points to the next packet, if fragmented. |
| 47 | * If the data is too large, the M_EXT is used, and a larger block |
| 48 | * is alloced. Therefore, m_free[m] must check for M_EXT and if set |
| 49 | * free the m_ext. This is inefficient memory-wise, but who cares. |
| 50 | */ |
| 51 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 52 | /* |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 53 | * How much room is in the mbuf, from m_data to the end of the mbuf |
| 54 | */ |
| 55 | #define M_ROOM(m) ((m->m_flags & M_EXT)? \ |
| 56 | (((m)->m_ext + (m)->m_size) - (m)->m_data) \ |
| 57 | : \ |
| 58 | (((m)->m_dat + (m)->m_size) - (m)->m_data)) |
| 59 | |
| 60 | /* |
| 61 | * How much free room there is |
| 62 | */ |
| 63 | #define M_FREEROOM(m) (M_ROOM(m) - (m)->m_len) |
| 64 | #define M_TRAILINGSPACE M_FREEROOM |
| 65 | |
| 66 | struct mbuf { |
Michael Tokarev | 0e44486 | 2013-07-13 13:10:05 +0400 | [diff] [blame] | 67 | /* XXX should union some of these! */ |
| 68 | /* header at beginning of each mbuf: */ |
| 69 | struct mbuf *m_next; /* Linked list of mbufs */ |
| 70 | struct mbuf *m_prev; |
| 71 | struct mbuf *m_nextpkt; /* Next packet in queue/record */ |
| 72 | struct mbuf *m_prevpkt; /* Flags aren't used in the output queue */ |
| 73 | int m_flags; /* Misc flags */ |
| 74 | |
| 75 | int m_size; /* Size of data */ |
| 76 | struct socket *m_so; |
| 77 | |
| 78 | caddr_t m_data; /* Location of data */ |
| 79 | int m_len; /* Amount of data in this mbuf */ |
| 80 | |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 81 | Slirp *slirp; |
Guillaume Subiron | fc3779a | 2015-12-19 22:24:56 +0100 | [diff] [blame] | 82 | bool resolution_requested; |
Thomas Huth | 2b44043 | 2011-09-27 11:20:38 +0200 | [diff] [blame] | 83 | uint64_t expiration_date; |
| 84 | /* start of dynamic buffer area, must be last element */ |
Michael Tokarev | 0e44486 | 2013-07-13 13:10:05 +0400 | [diff] [blame] | 85 | union { |
| 86 | char m_dat[1]; /* ANSI don't like 0 sized arrays */ |
| 87 | char *m_ext; |
| 88 | }; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 91 | #define ifq_prev m_prev |
| 92 | #define ifq_next m_next |
| 93 | #define ifs_prev m_prevpkt |
| 94 | #define ifs_next m_nextpkt |
| 95 | #define ifq_so m_so |
| 96 | |
| 97 | #define M_EXT 0x01 /* m_ext points to more (malloced) data */ |
| 98 | #define M_FREELIST 0x02 /* mbuf is on free list */ |
| 99 | #define M_USEDLIST 0x04 /* XXX mbuf is on used list (for dtom()) */ |
| 100 | #define M_DOFREE 0x08 /* when m_free is called on the mbuf, free() |
| 101 | * it rather than putting it on the free list */ |
| 102 | |
Blue Swirl | 6cb9c6d | 2009-07-01 19:11:17 +0000 | [diff] [blame] | 103 | void m_init(Slirp *); |
Jan Kiszka | a68adc2 | 2012-02-29 19:14:23 +0100 | [diff] [blame] | 104 | void m_cleanup(Slirp *slirp); |
Blue Swirl | 6cb9c6d | 2009-07-01 19:11:17 +0000 | [diff] [blame] | 105 | struct mbuf * m_get(Slirp *); |
| 106 | void m_free(struct mbuf *); |
| 107 | void m_cat(register struct mbuf *, register struct mbuf *); |
| 108 | void m_inc(struct mbuf *, int); |
| 109 | void m_adj(struct mbuf *, int); |
| 110 | int m_copy(struct mbuf *, struct mbuf *, int, int); |
| 111 | struct mbuf * dtom(Slirp *, void *); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 112 | |
Jan Kiszka | 79e7e93 | 2012-02-17 14:39:30 +0100 | [diff] [blame] | 113 | static inline void ifs_init(struct mbuf *ifm) |
| 114 | { |
| 115 | ifm->ifs_next = ifm->ifs_prev = ifm; |
| 116 | } |
| 117 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 118 | #endif |