VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/misc.c@ 34040

Last change on this file since 34040 was 34040, checked in by vboxsync, 14 years ago

NAT: more asserts.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.8 KB
Line 
1/* $Id: misc.c 34040 2010-11-12 18:52:01Z vboxsync $ */
2/** @file
3 * NAT - helpers.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/*
19 * This code is based on:
20 *
21 * Copyright (c) 1995 Danny Gasparovski.
22 *
23 * Please read the file COPYRIGHT for the
24 * terms and conditions of the copyright.
25 */
26
27#define WANT_SYS_IOCTL_H
28#include <slirp.h>
29
30#ifndef HAVE_INET_ATON
31int
32inet_aton(const char *cp, struct in_addr *ia)
33{
34 u_int32_t addr = inet_addr(cp);
35 if (addr == 0xffffffff)
36 return 0;
37 ia->s_addr = addr;
38 return 1;
39}
40#endif
41
42/*
43 * Get our IP address and put it in our_addr
44 */
45void
46getouraddr(PNATState pData)
47{
48 our_addr.s_addr = loopback_addr.s_addr;
49}
50
51struct quehead
52{
53 struct quehead *qh_link;
54 struct quehead *qh_rlink;
55};
56
57void
58insque(PNATState pData, void *a, void *b)
59{
60 register struct quehead *element = (struct quehead *) a;
61 register struct quehead *head = (struct quehead *) b;
62 element->qh_link = head->qh_link;
63 head->qh_link = (struct quehead *)element;
64 element->qh_rlink = (struct quehead *)head;
65 ((struct quehead *)(element->qh_link))->qh_rlink = (struct quehead *)element;
66}
67
68void
69remque(PNATState pData, void *a)
70{
71 register struct quehead *element = (struct quehead *) a;
72 ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
73 ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
74 element->qh_rlink = NULL;
75 /* element->qh_link = NULL; TCP FIN1 crashes if you do this. Why ? */
76}
77
78
79/*
80 * Set fd blocking and non-blocking
81 */
82void
83fd_nonblock(int fd)
84{
85#ifdef FIONBIO
86 int opt = 1;
87
88 ioctlsocket(fd, FIONBIO, &opt);
89#else
90 int opt;
91
92 opt = fcntl(fd, F_GETFL, 0);
93 opt |= O_NONBLOCK;
94 fcntl(fd, F_SETFL, opt);
95#endif
96}
97
98
99#define ITEM_MAGIC 0xdead0001
100struct item
101{
102 uint32_t magic;
103 uma_zone_t zone;
104 uint32_t ref_count;
105 LIST_ENTRY(item) list;
106};
107
108#define ZONE_MAGIC 0xdead0002
109struct uma_zone
110{
111 uint32_t magic;
112 PNATState pData; /* to minimize changes in the rest of UMA emulation code */
113 RTCRITSECT csZone;
114 const char *name;
115 size_t size; /* item size */
116 ctor_t pfCtor;
117 dtor_t pfDtor;
118 zinit_t pfInit;
119 zfini_t pfFini;
120 uma_alloc_t pfAlloc;
121 uma_free_t pfFree;
122 int max_items;
123 int cur_items;
124 LIST_HEAD(RT_NOTHING, item) used_items;
125 LIST_HEAD(RT_NOTHING, item) free_items;
126 uma_zone_t master_zone;
127 void *area;
128};
129
130
131static void *slirp_uma_alloc(uma_zone_t zone,
132 int size, uint8_t *pflags, int fWait)
133{
134 struct item *it;
135 uint8_t *sub_area;
136 void *ret = NULL;
137 int rc;
138
139 RTCritSectEnter(&zone->csZone);
140 for (;;)
141 {
142 if (!LIST_EMPTY(&zone->free_items))
143 {
144 it = LIST_FIRST(&zone->free_items);
145 Assert(it->magic == ITEM_MAGIC);
146 rc = 0;
147 if (zone->pfInit)
148 rc = zone->pfInit(zone->pData, (void *)&it[1], zone->size, M_DONTWAIT);
149 if (rc == 0)
150 {
151 zone->cur_items++;
152 LIST_REMOVE(it, list);
153 LIST_INSERT_HEAD(&zone->used_items, it, list);
154 ret = (void *)&it[1];
155 }
156 else
157 {
158 ret = NULL;
159 }
160 break;
161 }
162
163 if (!zone->master_zone)
164 {
165 /* We're on master zone and we cant allocate more */
166 Log2(("NAT: no room on %s zone\n", zone->name));
167 break;
168 }
169
170 /* we're on sub-zone we need get chunk of master zone and split
171 * it for sub-zone conforming chunks.
172 */
173 sub_area = slirp_uma_alloc(zone->master_zone, zone->master_zone->size, NULL, 0);
174 if (!sub_area)
175 {
176 /* No room on master */
177 Log2(("NAT: no room on %s zone for %s zone\n", zone->master_zone->name, zone->name));
178 break;
179 }
180 zone->max_items++;
181 it = &((struct item *)sub_area)[-1];
182 /* it's chunk descriptor of master zone we should remove it
183 * from the master list first
184 */
185 Assert((it->zone && it->zone->magic == ZONE_MAGIC));
186 RTCritSectEnter(&it->zone->csZone);
187 /* @todo should we alter count of master counters? */
188 LIST_REMOVE(it, list);
189 RTCritSectLeave(&it->zone->csZone);
190 /* @todo '+ zone->size' should be depend on flag */
191 memset(it, 0, sizeof(struct item));
192 it->zone = zone;
193 it->magic = ITEM_MAGIC;
194 LIST_INSERT_HEAD(&zone->free_items, it, list);
195 if (zone->cur_items >= zone->max_items)
196 LogRel(("NAT: zone(%s) has reached it maximum\n", zone->name));
197 }
198 RTCritSectLeave(&zone->csZone);
199 return ret;
200}
201
202static void slirp_uma_free(void *item, int size, uint8_t flags)
203{
204 struct item *it;
205 uma_zone_t zone;
206 uma_zone_t master_zone;
207 Assert(item);
208 it = &((struct item *)item)[-1];
209 Assert(it->magic == ITEM_MAGIC);
210 zone = it->zone;
211 /* check bourder magic */
212 Assert((*(uint32_t *)(((uint8_t *)&it[1]) + zone->size) == 0xabadbabe));
213 RTCritSectEnter(&zone->csZone);
214 Assert(zone->magic == ZONE_MAGIC);
215 LIST_REMOVE(it, list);
216 if (zone->pfFini)
217 {
218 zone->pfFini(zone->pData, item, zone->size);
219 }
220 if (zone->pfDtor)
221 {
222 zone->pfDtor(zone->pData, item, zone->size, NULL);
223 }
224 LIST_INSERT_HEAD(&zone->free_items, it, list);
225 zone->cur_items--;
226 RTCritSectLeave(&zone->csZone);
227}
228
229uma_zone_t uma_zcreate(PNATState pData, char *name, size_t size,
230 ctor_t ctor, dtor_t dtor, zinit_t init, zfini_t fini, int flags1, int flags2)
231{
232 uma_zone_t zone = RTMemAllocZ(sizeof(struct uma_zone));
233 Assert((pData));
234 zone->magic = ZONE_MAGIC;
235 zone->pData = pData;
236 zone->name = name;
237 zone->size = size;
238 zone->pfCtor = ctor;
239 zone->pfDtor = dtor;
240 zone->pfInit = init;
241 zone->pfFini = fini;
242 zone->pfAlloc = slirp_uma_alloc;
243 zone->pfFree = slirp_uma_free;
244 RTCritSectInit(&zone->csZone);
245 return zone;
246
247}
248uma_zone_t uma_zsecond_create(char *name, ctor_t ctor,
249 dtor_t dtor, zinit_t init, zfini_t fini, uma_zone_t master)
250{
251 uma_zone_t zone;
252 Assert(master);
253 zone = RTMemAllocZ(sizeof(struct uma_zone));
254 if (zone == NULL)
255 return NULL;
256
257 Assert((master && master->pData));
258 zone->magic = ZONE_MAGIC;
259 zone->pData = master->pData;
260 zone->name = name;
261 zone->pfCtor = ctor;
262 zone->pfDtor = dtor;
263 zone->pfInit = init;
264 zone->pfFini = fini;
265 zone->pfAlloc = slirp_uma_alloc;
266 zone->pfFree = slirp_uma_free;
267 zone->size = master->size;
268 zone->master_zone = master;
269 RTCritSectInit(&zone->csZone);
270 return zone;
271}
272
273void uma_zone_set_max(uma_zone_t zone, int max)
274{
275 int i = 0;
276 struct item *it;
277 zone->max_items = max;
278 zone->area = RTMemAllocZ(max * (sizeof(struct item) + zone->size + sizeof(uint32_t)));
279 for (; i < max; ++i)
280 {
281 it = (struct item *)(((uint8_t *)zone->area) + i*(sizeof(struct item) + zone->size + sizeof(uint32_t)));
282 it->magic = ITEM_MAGIC;
283 it->zone = zone;
284 *(uint32_t *)(((uint8_t *)&it[1]) + zone->size) = 0xabadbabe;
285 LIST_INSERT_HEAD(&zone->free_items, it, list);
286 }
287
288}
289
290void uma_zone_set_allocf(uma_zone_t zone, uma_alloc_t pfAlloc)
291{
292 zone->pfAlloc = pfAlloc;
293}
294
295void uma_zone_set_freef(uma_zone_t zone, uma_free_t pfFree)
296{
297 zone->pfFree = pfFree;
298}
299
300uint32_t *uma_find_refcnt(uma_zone_t zone, void *mem)
301{
302 /*@todo (vvl) this function supposed to work with special zone storing
303 reference counters */
304 struct item *it = (struct item *)mem; /* 1st element */
305 Assert(mem != NULL);
306 Assert(zone->magic == ZONE_MAGIC);
307 /* for returning pointer to counter we need get 0 elemnt */
308 Assert(it[-1].magic == ITEM_MAGIC);
309 return &it[-1].ref_count;
310}
311
312void *uma_zalloc_arg(uma_zone_t zone, void *args, int how)
313{
314 void *mem;
315 Assert(zone->magic == ZONE_MAGIC);
316 if (zone->pfAlloc == NULL)
317 return NULL;
318 RTCritSectEnter(&zone->csZone);
319 mem = zone->pfAlloc(zone, zone->size, NULL, 0);
320 if (mem != NULL)
321 {
322 if (zone->pfCtor)
323 zone->pfCtor(zone->pData, mem, zone->size, args, M_DONTWAIT);
324 }
325 RTCritSectLeave(&zone->csZone);
326 return mem;
327}
328
329void uma_zfree(uma_zone_t zone, void *item)
330{
331 uma_zfree_arg(zone, item, NULL);
332}
333
334void uma_zfree_arg(uma_zone_t zone, void *mem, void *flags)
335{
336 struct item *it;
337 Assert(zone->magic == ZONE_MAGIC);
338 Assert((zone->pfFree));
339 Assert((mem));
340
341 RTCritSectEnter(&zone->csZone);
342 it = &((struct item *)mem)[-1];
343 Assert((it->magic == ITEM_MAGIC));
344 Assert((zone->magic == ZONE_MAGIC && zone == it->zone));
345
346 zone->pfFree(mem, 0, 0);
347 RTCritSectLeave(&zone->csZone);
348}
349
350int uma_zone_exhausted_nolock(uma_zone_t zone)
351{
352 int fExhausted;
353 RTCritSectEnter(&zone->csZone);
354 fExhausted = (zone->cur_items == zone->max_items);
355 RTCritSectLeave(&zone->csZone);
356 return fExhausted;
357}
358
359void zone_drain(uma_zone_t zone)
360{
361 struct item *it;
362 uma_zone_t master_zone;
363 /* vvl: Huh? What to do with zone which hasn't got backstore ? */
364 Assert((zone->master_zone));
365 master_zone = zone->master_zone;
366 while(!LIST_EMPTY(&zone->free_items))
367 {
368 it = LIST_FIRST(&zone->free_items);
369 Assert((it->magic == ITEM_MAGIC));
370 RTCritSectEnter(&zone->csZone);
371 LIST_REMOVE(it, list);
372 zone->max_items--;
373 RTCritSectLeave(&zone->csZone);
374 it->zone = master_zone;
375 RTCritSectEnter(&master_zone->csZone);
376 LIST_INSERT_HEAD(&master_zone->free_items, it, list);
377 master_zone->cur_items--;
378 RTCritSectLeave(&master_zone->csZone);
379 }
380}
381
382void slirp_null_arg_free(void *mem, void *arg)
383{
384 /*@todo (r=vvl) make it wiser*/
385 Assert(mem);
386 RTMemFree(mem);
387}
388
389void *uma_zalloc(uma_zone_t zone, int len)
390{
391 return NULL;
392}
393
394struct mbuf *slirp_ext_m_get(PNATState pData, size_t cbMin, void **ppvBuf, size_t *pcbBuf)
395{
396 struct mbuf *m;
397 size_t size = MCLBYTES;
398 if (cbMin < MSIZE)
399 size = MCLBYTES;
400 else if (cbMin < MCLBYTES)
401 size = MCLBYTES;
402 else if (cbMin < MJUM9BYTES)
403 size = MJUM9BYTES;
404 else if (cbMin < MJUM16BYTES)
405 size = MJUM16BYTES;
406 else
407 AssertMsgFailed(("Unsupported size"));
408
409 m = m_getjcl(pData, M_NOWAIT, MT_HEADER, M_PKTHDR, size);
410 if (m == NULL)
411 {
412 *ppvBuf = NULL;
413 *pcbBuf = 0;
414 return NULL;
415 }
416 m->m_len = size;
417 *ppvBuf = mtod(m, void *);
418 *pcbBuf = size;
419 return m;
420}
421
422void slirp_ext_m_free(PNATState pData, struct mbuf *m, uint8_t *pu8Buf)
423{
424
425 if ( !pu8Buf
426 && pu8Buf != mtod(m, uint8_t *))
427 RTMemFree(pu8Buf); /* This buffer was allocated on heap */
428 m_freem(pData, m);
429}
430
431static void zone_destroy(uma_zone_t zone)
432{
433 RTCritSectEnter(&zone->csZone);
434 LogRel(("NAT: zone(nm:%s, used:%d)\n", zone->name, zone->cur_items));
435 if (zone->master_zone)
436 RTMemFree(zone->area);
437 RTCritSectLeave(&zone->csZone);
438 RTCritSectDelete(&zone->csZone);
439 RTMemFree(zone);
440}
441
442void m_fini(PNATState pData)
443{
444 zone_destroy(pData->zone_mbuf);
445 zone_destroy(pData->zone_clust);
446 zone_destroy(pData->zone_pack);
447 zone_destroy(pData->zone_jumbop);
448 zone_destroy(pData->zone_jumbo9);
449 zone_destroy(pData->zone_jumbo16);
450 /*@todo do finalize here.*/
451}
452
453void
454if_init(PNATState pData)
455{
456 /* 14 for ethernet */
457 if_maxlinkhdr = 14;
458 if_comp = IF_AUTOCOMP;
459 if_mtu = 1500;
460 if_mru = 1500;
461}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette