VirtualBox

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

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

NAT: clean up.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.6 KB
Line 
1/* $Id: misc.c 30016 2010-06-03 18:31:14Z 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 rc = 0;
146 if (zone->pfInit)
147 rc = zone->pfInit(zone->pData, (void *)&it[1], zone->size, M_DONTWAIT);
148 if (rc == 0)
149 {
150 zone->cur_items++;
151 LIST_REMOVE(it, list);
152 LIST_INSERT_HEAD(&zone->used_items, it, list);
153 ret = (void *)&it[1];
154 }
155 else
156 {
157 ret = NULL;
158 }
159 break;
160 }
161
162 if (!zone->master_zone)
163 {
164 /* We're on master zone and we cant allocate more */
165 Log2(("NAT: no room on %s zone\n", zone->name));
166 break;
167 }
168
169 /* we're on sub-zone we need get chunk of master zone and split
170 * it for sub-zone conforming chunks.
171 */
172 sub_area = slirp_uma_alloc(zone->master_zone, zone->master_zone->size, NULL, 0);
173 if (!sub_area)
174 {
175 /* No room on master */
176 Log2(("NAT: no room on %s zone for %s zone\n", zone->master_zone->name, zone->name));
177 break;
178 }
179 zone->max_items++;
180 it = &((struct item *)sub_area)[-1];
181 /* it's chunk descriptor of master zone we should remove it
182 * from the master list first
183 */
184 Assert((it->zone && it->zone->magic == ZONE_MAGIC));
185 RTCritSectEnter(&it->zone->csZone);
186 /* @todo should we alter count of master counters? */
187 LIST_REMOVE(it, list);
188 RTCritSectLeave(&it->zone->csZone);
189 /* @todo '+ zone->size' should be depend on flag */
190 memset(it, 0, sizeof(struct item));
191 it->zone = zone;
192 it->magic = ITEM_MAGIC;
193 LIST_INSERT_HEAD(&zone->free_items, it, list);
194 if (zone->cur_items >= zone->max_items)
195 LogRel(("NAT: zone(%s) has reached it maximum\n", zone->name));
196 }
197 RTCritSectLeave(&zone->csZone);
198 return ret;
199}
200
201static void slirp_uma_free(void *item, int size, uint8_t flags)
202{
203 struct item *it;
204 uma_zone_t zone;
205 uma_zone_t master_zone;
206 Assert(item);
207 it = &((struct item *)item)[-1];
208 Assert(it->magic == ITEM_MAGIC);
209 zone = it->zone;
210 /* check bourder magic */
211 Assert((*(uint32_t *)(((uint8_t *)&it[1]) + zone->size) == 0xabadbabe));
212 RTCritSectEnter(&zone->csZone);
213 Assert(zone->magic == ZONE_MAGIC);
214 LIST_REMOVE(it, list);
215 if (zone->pfFini)
216 {
217 zone->pfFini(zone->pData, item, zone->size);
218 }
219 if (zone->pfDtor)
220 {
221 zone->pfDtor(zone->pData, item, zone->size, NULL);
222 }
223 LIST_INSERT_HEAD(&zone->free_items, it, list);
224 zone->cur_items--;
225 RTCritSectLeave(&zone->csZone);
226}
227
228uma_zone_t uma_zcreate(PNATState pData, char *name, size_t size,
229 ctor_t ctor, dtor_t dtor, zinit_t init, zfini_t fini, int flags1, int flags2)
230{
231 uma_zone_t zone = RTMemAllocZ(sizeof(struct uma_zone));
232 Assert((pData));
233 zone->magic = ZONE_MAGIC;
234 zone->pData = pData;
235 zone->name = name;
236 zone->size = size;
237 zone->pfCtor = ctor;
238 zone->pfDtor = dtor;
239 zone->pfInit = init;
240 zone->pfFini = fini;
241 zone->pfAlloc = slirp_uma_alloc;
242 zone->pfFree = slirp_uma_free;
243 RTCritSectInit(&zone->csZone);
244 return zone;
245
246}
247uma_zone_t uma_zsecond_create(char *name, ctor_t ctor,
248 dtor_t dtor, zinit_t init, zfini_t fini, uma_zone_t master)
249{
250 uma_zone_t zone;
251 Assert(master);
252 zone = RTMemAllocZ(sizeof(struct uma_zone));
253 if (zone == NULL)
254 return NULL;
255
256 Assert((master && master->pData));
257 zone->magic = ZONE_MAGIC;
258 zone->pData = master->pData;
259 zone->name = name;
260 zone->pfCtor = ctor;
261 zone->pfDtor = dtor;
262 zone->pfInit = init;
263 zone->pfFini = fini;
264 zone->pfAlloc = slirp_uma_alloc;
265 zone->pfFree = slirp_uma_free;
266 zone->size = master->size;
267 zone->master_zone = master;
268 RTCritSectInit(&zone->csZone);
269 return zone;
270}
271
272void uma_zone_set_max(uma_zone_t zone, int max)
273{
274 int i = 0;
275 struct item *it;
276 zone->max_items = max;
277 zone->area = RTMemAllocZ(max * (sizeof(struct item) + zone->size + sizeof(uint32_t)));
278 for (; i < max; ++i)
279 {
280 it = (struct item *)(((uint8_t *)zone->area) + i*(sizeof(struct item) + zone->size + sizeof(uint32_t)));
281 it->magic = ITEM_MAGIC;
282 it->zone = zone;
283 *(uint32_t *)(((uint8_t *)&it[1]) + zone->size) = 0xabadbabe;
284 LIST_INSERT_HEAD(&zone->free_items, it, list);
285 }
286
287}
288
289void uma_zone_set_allocf(uma_zone_t zone, uma_alloc_t pfAlloc)
290{
291 zone->pfAlloc = pfAlloc;
292}
293
294void uma_zone_set_freef(uma_zone_t zone, uma_free_t pfFree)
295{
296 zone->pfFree = pfFree;
297}
298
299uint32_t *uma_find_refcnt(uma_zone_t zone, void *mem)
300{
301 /*@todo (vvl) this function supposed to work with special zone storing
302 reference counters */
303 struct item *it = (struct item *)mem; /* 1st element */
304 Assert(mem != NULL);
305 Assert(zone->magic == ZONE_MAGIC);
306 /* for returning pointer to counter we need get 0 elemnt */
307 Assert(it[-1].magic == ITEM_MAGIC);
308 return &it[-1].ref_count;
309}
310
311void *uma_zalloc_arg(uma_zone_t zone, void *args, int how)
312{
313 void *mem;
314 Assert(zone->magic == ZONE_MAGIC);
315 if (zone->pfAlloc == NULL)
316 return NULL;
317 RTCritSectEnter(&zone->csZone);
318 mem = zone->pfAlloc(zone, zone->size, NULL, 0);
319 if (mem != NULL)
320 {
321 if (zone->pfCtor)
322 zone->pfCtor(zone->pData, mem, zone->size, args, M_DONTWAIT);
323 }
324 RTCritSectLeave(&zone->csZone);
325 return mem;
326}
327
328void uma_zfree(uma_zone_t zone, void *item)
329{
330 uma_zfree_arg(zone, item, NULL);
331}
332
333void uma_zfree_arg(uma_zone_t zone, void *mem, void *flags)
334{
335 struct item *it;
336 Assert(zone->magic == ZONE_MAGIC);
337 Assert((zone->pfFree));
338 Assert((mem));
339
340 RTCritSectEnter(&zone->csZone);
341 it = &((struct item *)mem)[-1];
342 Assert((it->magic == ITEM_MAGIC));
343 Assert((zone->magic == ZONE_MAGIC && zone == it->zone));
344
345 zone->pfFree(mem, 0, 0);
346 RTCritSectLeave(&zone->csZone);
347}
348
349int uma_zone_exhausted_nolock(uma_zone_t zone)
350{
351 int fExhausted;
352 RTCritSectEnter(&zone->csZone);
353 fExhausted = (zone->cur_items == zone->max_items);
354 RTCritSectLeave(&zone->csZone);
355 return fExhausted;
356}
357
358void zone_drain(uma_zone_t zone)
359{
360 struct item *it;
361 uma_zone_t master_zone;
362 /* vvl: Huh? What to do with zone which hasn't got backstore ? */
363 Assert((zone->master_zone));
364 master_zone = zone->master_zone;
365 while(!LIST_EMPTY(&zone->free_items))
366 {
367 it = LIST_FIRST(&zone->free_items);
368 RTCritSectEnter(&zone->csZone);
369 LIST_REMOVE(it, list);
370 zone->max_items--;
371 RTCritSectLeave(&zone->csZone);
372 it->zone = master_zone;
373 RTCritSectEnter(&master_zone->csZone);
374 LIST_INSERT_HEAD(&master_zone->free_items, it, list);
375 master_zone->cur_items--;
376 RTCritSectLeave(&master_zone->csZone);
377 }
378}
379
380void slirp_null_arg_free(void *mem, void *arg)
381{
382 /*@todo (r=vvl) make it wiser*/
383 Assert(mem);
384 RTMemFree(mem);
385}
386
387void *uma_zalloc(uma_zone_t zone, int len)
388{
389 return NULL;
390}
391
392struct mbuf *slirp_ext_m_get(PNATState pData, size_t cbMin, void **ppvBuf, size_t *pcbBuf)
393{
394 struct mbuf *m;
395 size_t size = MCLBYTES;
396 if (cbMin < MSIZE)
397 size = MCLBYTES;
398 else if (cbMin < MCLBYTES)
399 size = MCLBYTES;
400 else if (cbMin < MJUM9BYTES)
401 size = MJUM9BYTES;
402 else if (cbMin < MJUM16BYTES)
403 size = MJUM16BYTES;
404 else
405 AssertMsgFailed(("Unsupported size"));
406
407 m = m_getjcl(pData, M_NOWAIT, MT_HEADER, M_PKTHDR, size);
408 if (m == NULL)
409 {
410 *ppvBuf = NULL;
411 *pcbBuf = 0;
412 return NULL;
413 }
414 m->m_len = size;
415 *ppvBuf = mtod(m, void *);
416 *pcbBuf = size;
417 return m;
418}
419
420void slirp_ext_m_free(PNATState pData, struct mbuf *m)
421{
422 m_freem(pData, m);
423}
424
425static void zone_destroy(uma_zone_t zone)
426{
427 RTCritSectEnter(&zone->csZone);
428 LogRel(("NAT: zone(nm:%s, used:%d)\n", zone->name, zone->cur_items));
429 if (zone->master_zone)
430 RTMemFree(zone->area);
431 RTCritSectLeave(&zone->csZone);
432 RTCritSectDelete(&zone->csZone);
433 RTMemFree(zone);
434}
435
436void m_fini(PNATState pData)
437{
438 zone_destroy(pData->zone_mbuf);
439 zone_destroy(pData->zone_clust);
440 zone_destroy(pData->zone_pack);
441 zone_destroy(pData->zone_jumbop);
442 zone_destroy(pData->zone_jumbo9);
443 zone_destroy(pData->zone_jumbo16);
444 /*@todo do finalize here.*/
445}
446
447void
448if_init(PNATState pData)
449{
450 /* 14 for ethernet */
451 if_maxlinkhdr = 14;
452 if_comp = IF_AUTOCOMP;
453 if_mtu = 1500;
454 if_mru = 1500;
455}
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