VirtualBox

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

Last change on this file since 28800 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.5 KB
Line 
1/* $Id: misc.c 28800 2010-04-27 08:22:32Z 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#ifdef VBOX_WITH_SLIRP_BSD_MBUF
100#define ITEM_MAGIC 0xdead0001
101struct item
102{
103 uint32_t magic;
104 uma_zone_t zone;
105 uint32_t ref_count;
106 LIST_ENTRY(item) list;
107};
108
109#define ZONE_MAGIC 0xdead0002
110struct uma_zone
111{
112 uint32_t magic;
113 PNATState pData; /* to minimize changes in the rest of UMA emulation code */
114 RTCRITSECT csZone;
115 const char *name;
116 size_t size; /* item size */
117 ctor_t pfCtor;
118 dtor_t pfDtor;
119 zinit_t pfInit;
120 zfini_t pfFini;
121 uma_alloc_t pfAlloc;
122 uma_free_t pfFree;
123 int max_items;
124 int cur_items;
125 LIST_HEAD(RT_NOTHING, item) used_items;
126 LIST_HEAD(RT_NOTHING, item) free_items;
127 uma_zone_t master_zone;
128 void *area;
129};
130
131
132static void *slirp_uma_alloc(uma_zone_t zone,
133 int size, uint8_t *pflags, int fWait)
134{
135 struct item *it;
136 uint8_t *sub_area;
137 void *ret = NULL;
138 int rc;
139
140 RTCritSectEnter(&zone->csZone);
141 for (;;)
142 {
143 if (!LIST_EMPTY(&zone->free_items))
144 {
145 it = LIST_FIRST(&zone->free_items);
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 RTCritSectEnter(&zone->csZone);
370 LIST_REMOVE(it, list);
371 zone->max_items--;
372 RTCritSectLeave(&zone->csZone);
373 it->zone = master_zone;
374 RTCritSectEnter(&master_zone->csZone);
375 LIST_INSERT_HEAD(&master_zone->free_items, it, list);
376 master_zone->cur_items--;
377 RTCritSectLeave(&master_zone->csZone);
378 }
379}
380
381void slirp_null_arg_free(void *mem, void *arg)
382{
383 /*@todo (r=vvl) make it wiser*/
384 Assert(mem);
385 RTMemFree(mem);
386}
387
388void *uma_zalloc(uma_zone_t zone, int len)
389{
390 return NULL;
391}
392
393struct mbuf *slirp_ext_m_get(PNATState pData, size_t cbMin, void **ppvBuf, size_t *pcbBuf)
394{
395 struct mbuf *m;
396 size_t size = MCLBYTES;
397 if (cbMin < MSIZE)
398 size = MCLBYTES;
399 else if (cbMin < MCLBYTES)
400 size = MCLBYTES;
401 else if (cbMin < MJUM9BYTES)
402 size = MJUM9BYTES;
403 else if (cbMin < MJUM16BYTES)
404 size = MJUM16BYTES;
405 else
406 AssertMsgFailed(("Unsupported size"));
407
408 m = m_getjcl(pData, M_NOWAIT, MT_HEADER, M_PKTHDR, size);
409 if (m == NULL)
410 {
411 *ppvBuf = NULL;
412 *pcbBuf = 0;
413 return NULL;
414 }
415 m->m_len = size;
416 *ppvBuf = mtod(m, void *);
417 *pcbBuf = size;
418 return m;
419}
420
421void slirp_ext_m_free(PNATState pData, struct mbuf *m)
422{
423 m_freem(pData, m);
424}
425
426static void zone_destroy(uma_zone_t zone)
427{
428 RTCritSectEnter(&zone->csZone);
429 LogRel(("NAT: zone(nm:%s, used:%d)\n", zone->name, zone->cur_items));
430 if (zone->master_zone)
431 RTMemFree(zone->area);
432 RTCritSectLeave(&zone->csZone);
433 RTCritSectDelete(&zone->csZone);
434 RTMemFree(zone);
435}
436
437void m_fini(PNATState pData)
438{
439 zone_destroy(pData->zone_mbuf);
440 zone_destroy(pData->zone_clust);
441 zone_destroy(pData->zone_pack);
442 zone_destroy(pData->zone_jumbop);
443 zone_destroy(pData->zone_jumbo9);
444 zone_destroy(pData->zone_jumbo16);
445 /*@todo do finalize here.*/
446}
447#endif /* VBOX_WITH_SLIRP_BSD_MBUF */
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