aboutsummaryrefslogtreecommitdiffstats
path: root/slirp/mbuf.c
blob: efc8141059b222376d5ce51d90e9111e94d5f426 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * Copyright (c) 1995 Danny Gasparovski
 *
 * Please read the file COPYRIGHT for the
 * terms and conditions of the copyright.
 */

/*
 * mbuf's in SLiRP are much simpler than the real mbufs in
 * FreeBSD.  They are fixed size, determined by the MTU,
 * so that one whole packet can fit.  Mbuf's cannot be
 * chained together.  If there's more data than the mbuf
 * could hold, an external malloced buffer is pointed to
 * by m_ext (and the data pointers) and M_EXT is set in
 * the flags
 */

#include <slirp.h>

static int      mbuf_alloced = 0;
static MBufRec  m_freelist, m_usedlist;
static int      mbuf_thresh = 30;
static int      mbuf_max = 0;
static int      msize;

/* 
 * How much room is in the mbuf, from m_data to the end of the mbuf
 */
#define M_ROOM(m) ((m->m_flags & M_EXT)? \
			(((m)->m_ext + (m)->m_size) - (m)->m_data) \
		   : \
			(((m)->m_dat + (m)->m_size) - (m)->m_data))

/*
 * How much free room there is
 */
#define M_FREEROOM(m) (M_ROOM(m) - (m)->m_len)


void
mbuf_init()
{
	m_freelist.m_next = m_freelist.m_prev = &m_freelist;
	m_usedlist.m_next = m_usedlist.m_prev = &m_usedlist;
	msize_init();
}

void
msize_init()
{
	/*
	 * Find a nice value for msize
	 * XXX if_maxlinkhdr already in mtu
	 */
	msize = (if_mtu > if_mru ? if_mtu : if_mru) + 
			if_maxlinkhdr + sizeof(struct m_hdr ) + 6;
}

static void
mbuf_insque(MBuf  m, MBuf  head)
{
	m->m_next         = head->m_next;
	m->m_prev         = head;
	head->m_next      = m;
	m->m_next->m_prev = m;
}

static void
mbuf_remque(MBuf  m)
{
	m->m_prev->m_next = m->m_next;
	m->m_next->m_prev = m->m_prev;
	m->m_next = m->m_prev = m;
}

/*
 * Get an mbuf from the free list, if there are none
 * malloc one
 * 
 * Because fragmentation can occur if we alloc new mbufs and
 * free old mbufs, we mark all mbufs above mbuf_thresh as M_DOFREE,
 * which tells m_free to actually free() it
 */
MBuf
mbuf_alloc(void)
{
	register MBuf m;
	int flags = 0;
	
	DEBUG_CALL("mbuf_alloc");
	
	if (m_freelist.m_next == &m_freelist) {
		m = (MBuf) malloc(msize);
		if (m == NULL) goto end_error;
		mbuf_alloced++;
		if (mbuf_alloced > mbuf_thresh)
			flags = M_DOFREE;
		if (mbuf_alloced > mbuf_max)
			mbuf_max = mbuf_alloced;
	} else {
		m = m_freelist.m_next;
		mbuf_remque(m);
	}
	
	/* Insert it in the used list */
	mbuf_insque(m,&m_usedlist);
	m->m_flags = (flags | M_USEDLIST);
	
	/* Initialise it */
	m->m_size  = msize - sizeof(struct m_hdr);
	m->m_data  = m->m_dat;
	m->m_len   = 0;
	m->m_next2 = NULL;
	m->m_prev2 = NULL;
end_error:
	DEBUG_ARG("m = %lx", (long )m);
	return m;
}

void
mbuf_free(MBuf  m)
{
	
  DEBUG_CALL("mbuf_free");
  DEBUG_ARG("m = %lx", (long )m);
	
  if(m) {
	/* Remove from m_usedlist */
	if (m->m_flags & M_USEDLIST)
	   mbuf_remque(m);
	
	/* If it's M_EXT, free() it */
	if (m->m_flags & M_EXT)
	   free(m->m_ext);

	/*
	 * Either free() it or put it on the free list
	 */
	if (m->m_flags & M_DOFREE) {
		free(m);
		mbuf_alloced--;
	} else if ((m->m_flags & M_FREELIST) == 0) {
		mbuf_insque(m,&m_freelist);
		m->m_flags = M_FREELIST; /* Clobber other flags */
	}
  } /* if(m) */
}

/*
 * Copy data from one mbuf to the end of
 * the other.. if result is too big for one mbuf, malloc()
 * an M_EXT data segment
 */
void
mbuf_append(MBuf  m, MBuf  n)
{
	/*
	 * If there's no room, realloc
	 */
	if (M_FREEROOM(m) < n->m_len)
		mbuf_ensure(m, m->m_size+MINCSIZE);
	
	memcpy(m->m_data+m->m_len, n->m_data, n->m_len);
	m->m_len += n->m_len;

	mbuf_free(n);
}


/* make m size bytes large */
void
mbuf_ensure(MBuf  m, int  size)
{
	int datasize;

	/* some compiles throw up on gotos.  This one we can fake. */
    if(m->m_size > size) return;

    if (m->m_flags & M_EXT) {
        datasize = m->m_data - m->m_ext;
        m->m_ext = (char *)realloc(m->m_ext,size);
        m->m_data = m->m_ext + datasize;
    } else {
        char *dat;
        datasize = m->m_data - m->m_dat;
        dat      = (char *)malloc(size);
        memcpy(dat, m->m_dat, m->m_size);

        m->m_ext    = dat;
        m->m_data   = m->m_ext + datasize;
        m->m_flags |= M_EXT;
    }
 
    m->m_size = size;
}



void
mbuf_trim(MBuf  m, int  len)
{
	if (m == NULL)
		return;
	if (len >= 0) {
		/* Trim from head */
		m->m_data += len;
		m->m_len  -= len;
	} else {
		/* Trim from tail */
		len       = -len;
		m->m_len -= len;
	}
}


/*
 * Copy len bytes from m, starting off bytes into n
 */
int
mbuf_copy(MBuf  n, MBuf  m, int  off, int  len)
{
	if (len > M_FREEROOM(n))
		return -1;

	memcpy((n->m_data + n->m_len), (m->m_data + off), len);
	n->m_len += len;
	return 0;
}

int
mbuf_freeroom( MBuf  m )
{
	return  M_FREEROOM(m);
}

/*
 * Given a pointer into an mbuf, return the mbuf
 * XXX This is a kludge, I should eliminate the need for it
 * Fortunately, it's not used often
 */
MBuf
mbuf_from(void*  dat)
{
	MBuf  m;
	
	DEBUG_CALL("mbuf_from");
	DEBUG_ARG("dat = %lx", (long )dat);

	/* bug corrected for M_EXT buffers */
	for (m = m_usedlist.m_next; m != &m_usedlist; m = m->m_next) {
	  if (m->m_flags & M_EXT) {
	    if( (unsigned)((char*)dat - m->m_ext) < (unsigned)m->m_size )
			goto Exit;
	  } else {
	    if( (unsigned)((char *)dat - m->m_dat) < (unsigned)m->m_size )
	      goto Exit;
	  }
	}
	m  = NULL;
	DEBUG_ERROR((dfd, "mbuf_from failed"));
Exit:	
	return m;
}

void
mbufstats()
{
	MBuf m;
	int i;
	
    lprint(" \r\n");
	
	lprint("Mbuf stats:\r\n");

	lprint("  %6d mbufs allocated (%d max)\r\n", mbuf_alloced, mbuf_max);
	
	i = 0;
	for (m = m_freelist.m_next; m != &m_freelist; m = m->m_next)
		i++;
	lprint("  %6d mbufs on free list\r\n",  i);
	
	i = 0;
	for (m = m_usedlist.m_next; m != &m_usedlist; m = m->m_next)
		i++;
	lprint("  %6d mbufs on used list\r\n",  i);
        lprint("  %6d mbufs queued as packets\r\n\r\n", if_queued);
}