VirtualBox

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

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

NAT: out-of-memory feedback (xTracker/5103)

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