VirtualBox

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

Last change on this file since 97441 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.4 KB
Line 
1/* $Id: misc.c 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * NAT - helpers.
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28/*
29 * This code is based on:
30 *
31 * Copyright (c) 1995 Danny Gasparovski.
32 *
33 * Please read the file COPYRIGHT for the
34 * terms and conditions of the copyright.
35 */
36
37#ifndef VBOX_NAT_TST_QUEUE
38#include <slirp.h>
39#include "zone.h"
40
41# ifndef HAVE_INET_ATON
42int
43inet_aton(const char *cp, struct in_addr *ia)
44{
45 u_int32_t addr = inet_addr(cp);
46 if (addr == 0xffffffff)
47 return 0;
48 ia->s_addr = addr;
49 return 1;
50}
51# endif
52
53/*
54 * Get our IP address and put it in our_addr
55 */
56void
57getouraddr(PNATState pData)
58{
59 our_addr.s_addr = loopback_addr.s_addr;
60}
61#else /* VBOX_NAT_TST_QUEUE */
62# include <iprt/cdefs.h>
63# include <iprt/types.h>
64# include "misc.h"
65#endif
66struct quehead
67{
68 struct quehead *qh_link;
69 struct quehead *qh_rlink;
70};
71
72void
73insque(PNATState pData, void *a, void *b)
74{
75 register struct quehead *element = (struct quehead *) a;
76 register struct quehead *head = (struct quehead *) b;
77 NOREF(pData);
78 element->qh_link = head->qh_link;
79 head->qh_link = (struct quehead *)element;
80 element->qh_rlink = (struct quehead *)head;
81 ((struct quehead *)(element->qh_link))->qh_rlink = (struct quehead *)element;
82}
83
84void
85remque(PNATState pData, void *a)
86{
87 register struct quehead *element = (struct quehead *) a;
88 NOREF(pData);
89 ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
90 ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
91 element->qh_rlink = NULL;
92 /* element->qh_link = NULL; TCP FIN1 crashes if you do this. Why ? */
93}
94
95#ifndef VBOX_NAT_TST_QUEUE
96
97/*
98 * Set fd blocking and non-blocking
99 */
100void
101fd_nonblock(int fd)
102{
103# ifdef FIONBIO
104# ifdef RT_OS_WINDOWS
105 u_long opt = 1;
106# else
107 int opt = 1;
108# endif
109 ioctlsocket(fd, FIONBIO, &opt);
110# else /* !FIONBIO */
111 int opt;
112
113 opt = fcntl(fd, F_GETFL, 0);
114 opt |= O_NONBLOCK;
115 fcntl(fd, F_SETFL, opt);
116# endif
117}
118
119
120# if defined(VBOX_NAT_MEM_DEBUG)
121# define NATMEM_LOG_FLOW_FUNC(a) LogFlowFunc(a)
122# define NATMEM_LOG_FLOW_FUNC_ENTER() LogFlowFuncEnter()
123# define NATMEM_LOG_FLOW_FUNC_LEAVE() LogFlowFuncLeave()
124# define NATMEM_LOG_2(a) Log2(a)
125# else
126# define NATMEM_LOG_FLOW_FUNC(a) do { } while (0)
127# define NATMEM_LOG_FLOW_FUNC_ENTER() do { } while (0)
128# define NATMEM_LOG_FLOW_FUNC_LEAVE() do { } while (0)
129# define NATMEM_LOG_2(a) do { } while (0)
130# endif
131
132
133/**
134 * Called when memory becomes available, works pfnXmitPending.
135 *
136 * @note This will LEAVE the critical section of the zone and RE-ENTER it
137 * again. Changes to the zone data should be expected across calls to
138 * this function!
139 *
140 * @param zone The zone.
141 */
142DECLINLINE(void) slirp_zone_check_and_send_pending(uma_zone_t zone)
143{
144 NATMEM_LOG_FLOW_FUNC(("ENTER: zone:%R[mzone]\n", zone));
145 if ( zone->fDoXmitPending
146 && zone->master_zone == NULL)
147 {
148 int rc2;
149 zone->fDoXmitPending = false;
150 rc2 = RTCritSectLeave(&zone->csZone); AssertRC(rc2);
151
152 slirp_output_pending(zone->pData->pvUser);
153
154 rc2 = RTCritSectEnter(&zone->csZone); AssertRC(rc2);
155 }
156 NATMEM_LOG_FLOW_FUNC_LEAVE();
157}
158
159static void *slirp_uma_alloc(uma_zone_t zone,
160 int size, uint8_t *pflags, int fWait)
161{
162 struct item *it;
163 uint8_t *sub_area;
164 void *ret = NULL;
165 int rc;
166
167 NATMEM_LOG_FLOW_FUNC(("ENTER: %R[mzone], size:%d, pflags:%p, %RTbool\n", zone, size, pflags, fWait)); RT_NOREF(size, pflags, fWait);
168 RTCritSectEnter(&zone->csZone);
169 for (;;)
170 {
171 if (!LIST_EMPTY(&zone->free_items))
172 {
173 it = LIST_FIRST(&zone->free_items);
174 Assert(it->magic == ITEM_MAGIC);
175 rc = 0;
176 if (zone->pfInit)
177 rc = zone->pfInit(zone->pData, (void *)&it[1], (int /*sigh*/)zone->size, M_DONTWAIT);
178 if (rc == 0)
179 {
180 zone->cur_items++;
181 LIST_REMOVE(it, list);
182 LIST_INSERT_HEAD(&zone->used_items, it, list);
183 slirp_zone_check_and_send_pending(zone); /* may exit+enter the cs! */
184 ret = (void *)&it[1];
185 }
186 else
187 {
188 AssertMsgFailed(("NAT: item initialization failed for zone %s\n", zone->name));
189 ret = NULL;
190 }
191 break;
192 }
193
194 if (!zone->master_zone)
195 {
196 /* We're on the master zone and we can't allocate more. */
197 NATMEM_LOG_2(("NAT: no room on %s zone\n", zone->name));
198 /* AssertMsgFailed(("NAT: OOM!")); */
199 zone->fDoXmitPending = true;
200 break;
201 }
202
203 /* we're on a sub-zone, we need get a chunk from the master zone and split
204 * it into sub-zone conforming chunks.
205 */
206 sub_area = slirp_uma_alloc(zone->master_zone, (int /*sigh*/)zone->master_zone->size, NULL, 0);
207 if (!sub_area)
208 {
209 /* No room on master */
210 NATMEM_LOG_2(("NAT: no room on %s zone for %s zone\n", zone->master_zone->name, zone->name));
211 break;
212 }
213 zone->max_items++;
214 it = &((struct item *)sub_area)[-1];
215 /* It's the chunk descriptor of the master zone, we should remove it
216 * from the master list first.
217 */
218 Assert((it->zone && it->zone->magic == ZONE_MAGIC));
219 RTCritSectEnter(&it->zone->csZone);
220 /** @todo should we alter count of master counters? */
221 LIST_REMOVE(it, list);
222 RTCritSectLeave(&it->zone->csZone);
223
224 /** @todo '+ zone->size' should be depend on flag */
225 memset(it, 0, sizeof(struct item));
226 it->zone = zone;
227 it->magic = ITEM_MAGIC;
228 LIST_INSERT_HEAD(&zone->free_items, it, list);
229 if (zone->cur_items >= zone->max_items)
230 LogRel(("NAT: Zone(%s) has reached it maximum\n", zone->name));
231 }
232 RTCritSectLeave(&zone->csZone);
233 NATMEM_LOG_FLOW_FUNC(("LEAVE: %p\n", ret));
234 return ret;
235}
236
237static void slirp_uma_free(void *item, int size, uint8_t flags)
238{
239 struct item *it;
240 uma_zone_t zone;
241
242 Assert(item);
243 it = &((struct item *)item)[-1];
244 NATMEM_LOG_FLOW_FUNC(("ENTER: item:%p(%R[mzoneitem]), size:%d, flags:%RX8\n", item, it, size, flags)); RT_NOREF(size, flags);
245 Assert(it->magic == ITEM_MAGIC);
246 zone = it->zone;
247 /* check border magic */
248 Assert((*(uint32_t *)(((uint8_t *)&it[1]) + zone->size) == 0xabadbabe));
249
250 RTCritSectEnter(&zone->csZone);
251 Assert(zone->magic == ZONE_MAGIC);
252 LIST_REMOVE(it, list);
253 if (zone->pfFini)
254 {
255 zone->pfFini(zone->pData, item, (int /*sigh*/)zone->size);
256 }
257 if (zone->pfDtor)
258 {
259 zone->pfDtor(zone->pData, item, (int /*sigh*/)zone->size, NULL);
260 }
261 LIST_INSERT_HEAD(&zone->free_items, it, list);
262 zone->cur_items--;
263 slirp_zone_check_and_send_pending(zone); /* may exit+enter the cs! */
264 RTCritSectLeave(&zone->csZone);
265 NATMEM_LOG_FLOW_FUNC_LEAVE();
266}
267
268uma_zone_t uma_zcreate(PNATState pData, char *name, size_t size,
269 ctor_t ctor, dtor_t dtor, zinit_t init, zfini_t fini, int flags1, int flags2)
270{
271 uma_zone_t zone = NULL;
272 NATMEM_LOG_FLOW_FUNC(("ENTER: name:%s size:%d, ctor:%p, dtor:%p, init:%p, fini:%p, flags1:%RX32, flags2:%RX32\n",
273 name, ctor, dtor, init, fini, flags1, flags2)); RT_NOREF(flags1, flags2);
274 zone = RTMemAllocZ(sizeof(struct uma_zone));
275 Assert((pData));
276 zone->magic = ZONE_MAGIC;
277 zone->pData = pData;
278 zone->name = name;
279 zone->size = size;
280 zone->pfCtor = ctor;
281 zone->pfDtor = dtor;
282 zone->pfInit = init;
283 zone->pfFini = fini;
284 zone->pfAlloc = slirp_uma_alloc;
285 zone->pfFree = slirp_uma_free;
286 RTCritSectInit(&zone->csZone);
287 NATMEM_LOG_FLOW_FUNC(("LEAVE: %R[mzone]\n", zone));
288 return zone;
289
290}
291uma_zone_t uma_zsecond_create(char *name, ctor_t ctor,
292 dtor_t dtor, zinit_t init, zfini_t fini, uma_zone_t master)
293{
294 uma_zone_t zone;
295 Assert(master);
296 NATMEM_LOG_FLOW_FUNC(("ENTER: name:%s ctor:%p, dtor:%p, init:%p, fini:%p, master:%R[mzone]\n",
297 name, ctor, dtor, init, fini, master));
298 zone = RTMemAllocZ(sizeof(struct uma_zone));
299 if (zone == NULL)
300 {
301 NATMEM_LOG_FLOW_FUNC(("LEAVE: %R[mzone]\n", NULL));
302 return NULL;
303 }
304
305 Assert((master && master->pData));
306 zone->magic = ZONE_MAGIC;
307 zone->pData = master->pData;
308 zone->name = name;
309 zone->pfCtor = ctor;
310 zone->pfDtor = dtor;
311 zone->pfInit = init;
312 zone->pfFini = fini;
313 zone->pfAlloc = slirp_uma_alloc;
314 zone->pfFree = slirp_uma_free;
315 zone->size = master->size;
316 zone->master_zone = master;
317 RTCritSectInit(&zone->csZone);
318 NATMEM_LOG_FLOW_FUNC(("LEAVE: %R[mzone]\n", zone));
319 return zone;
320}
321
322void uma_zone_set_max(uma_zone_t zone, int max)
323{
324 int i = 0;
325 struct item *it;
326 NATMEM_LOG_FLOW_FUNC(("ENTER: zone:%R[mzone], max:%d\n", zone, max));
327 zone->max_items = max;
328 zone->area = RTMemAllocZ(max * (sizeof(struct item) + zone->size + sizeof(uint32_t)));
329 for (; i < max; ++i)
330 {
331 it = (struct item *)(((uint8_t *)zone->area) + i*(sizeof(struct item) + zone->size + sizeof(uint32_t)));
332 it->magic = ITEM_MAGIC;
333 it->zone = zone;
334 *(uint32_t *)(((uint8_t *)&it[1]) + zone->size) = 0xabadbabe;
335 LIST_INSERT_HEAD(&zone->free_items, it, list);
336 }
337 NATMEM_LOG_FLOW_FUNC_LEAVE();
338}
339
340void uma_zone_set_allocf(uma_zone_t zone, uma_alloc_t pfAlloc)
341{
342 NATMEM_LOG_FLOW_FUNC(("ENTER: zone:%R[mzone], pfAlloc:%Rfn\n", zone, pfAlloc));
343 zone->pfAlloc = pfAlloc;
344 NATMEM_LOG_FLOW_FUNC_LEAVE();
345}
346
347void uma_zone_set_freef(uma_zone_t zone, uma_free_t pfFree)
348{
349 NATMEM_LOG_FLOW_FUNC(("ENTER: zone:%R[mzone], pfAlloc:%Rfn\n", zone, pfFree));
350 zone->pfFree = pfFree;
351 NATMEM_LOG_FLOW_FUNC_LEAVE();
352}
353
354uint32_t *uma_find_refcnt(uma_zone_t zone, void *mem)
355{
356 /** @todo (vvl) this function supposed to work with special zone storing
357 reference counters */
358 struct item *it = NULL;
359 NATMEM_LOG_FLOW_FUNC(("ENTER: zone:%R[mzone], mem:%p\n", zone, mem)); RT_NOREF(zone);
360 it = (struct item *)mem; /* 1st element */
361 Assert(mem != NULL);
362 Assert(zone->magic == ZONE_MAGIC);
363 /* for returning pointer to counter we need get 0 elemnt */
364 Assert(it[-1].magic == ITEM_MAGIC);
365 NATMEM_LOG_FLOW_FUNC(("LEAVE: %p\n", &it[-1].ref_count));
366 return &it[-1].ref_count;
367}
368
369void *uma_zalloc_arg(uma_zone_t zone, void *args, int how)
370{
371 void *mem;
372 Assert(zone->magic == ZONE_MAGIC);
373 NATMEM_LOG_FLOW_FUNC(("ENTER: zone:%R[mzone], args:%p, how:%RX32\n", zone, args, how)); RT_NOREF(how);
374 if (zone->pfAlloc == NULL)
375 {
376 NATMEM_LOG_FLOW_FUNC(("LEAVE: NULL\n"));
377 return NULL;
378 }
379 RTCritSectEnter(&zone->csZone);
380 mem = zone->pfAlloc(zone, (int /*sigh*/)zone->size, NULL, 0);
381 if (mem != NULL)
382 {
383 if (zone->pfCtor)
384 zone->pfCtor(zone->pData, mem, (int /*sigh*/)zone->size, args, M_DONTWAIT);
385 }
386 RTCritSectLeave(&zone->csZone);
387 NATMEM_LOG_FLOW_FUNC(("LEAVE: %p\n", mem));
388 return mem;
389}
390
391void uma_zfree(uma_zone_t zone, void *item)
392{
393 NATMEM_LOG_FLOW_FUNC(("ENTER: zone:%R[mzone], item:%p\n", zone, item));
394 uma_zfree_arg(zone, item, NULL);
395 NATMEM_LOG_FLOW_FUNC_LEAVE();
396}
397
398void uma_zfree_arg(uma_zone_t zone, void *mem, void *flags)
399{
400 struct item *it;
401 Assert(zone->magic == ZONE_MAGIC);
402 Assert((zone->pfFree));
403 Assert((mem));
404 NATMEM_LOG_FLOW_FUNC(("ENTER: zone:%R[mzone], mem:%p, flags:%p\n", zone, mem, flags)); RT_NOREF(flags);
405
406 RTCritSectEnter(&zone->csZone);
407 it = &((struct item *)mem)[-1];
408 Assert((it->magic == ITEM_MAGIC));
409 Assert((zone->magic == ZONE_MAGIC && zone == it->zone));
410
411 zone->pfFree(mem, 0, 0);
412 RTCritSectLeave(&zone->csZone);
413
414 NATMEM_LOG_FLOW_FUNC_LEAVE();
415}
416
417int uma_zone_exhausted_nolock(uma_zone_t zone)
418{
419 int fExhausted;
420 NATMEM_LOG_FLOW_FUNC(("ENTER: zone:%R[mzone]\n", zone));
421 RTCritSectEnter(&zone->csZone);
422 fExhausted = (zone->cur_items == zone->max_items);
423 RTCritSectLeave(&zone->csZone);
424 NATMEM_LOG_FLOW_FUNC(("LEAVE: %RTbool\n", fExhausted));
425 return fExhausted;
426}
427
428void zone_drain(uma_zone_t zone)
429{
430 struct item *it;
431 uma_zone_t master_zone;
432
433 /* vvl: Huh? What to do with zone which hasn't got backstore ? */
434 Assert((zone->master_zone));
435 NATMEM_LOG_FLOW_FUNC(("ENTER: zone:%R[mzone]\n", zone));
436 master_zone = zone->master_zone;
437 while (!LIST_EMPTY(&zone->free_items))
438 {
439 it = LIST_FIRST(&zone->free_items);
440 Assert((it->magic == ITEM_MAGIC));
441
442 RTCritSectEnter(&zone->csZone);
443 LIST_REMOVE(it, list);
444 zone->max_items--;
445 RTCritSectLeave(&zone->csZone);
446
447 it->zone = master_zone;
448
449 RTCritSectEnter(&master_zone->csZone);
450 LIST_INSERT_HEAD(&master_zone->free_items, it, list);
451 master_zone->cur_items--;
452 slirp_zone_check_and_send_pending(master_zone); /* may exit+enter the cs! */
453 RTCritSectLeave(&master_zone->csZone);
454 }
455 NATMEM_LOG_FLOW_FUNC_LEAVE();
456}
457
458void slirp_null_arg_free(void *mem, void *arg)
459{
460 /** @todo (vvl) make it wiser */
461 NATMEM_LOG_FLOW_FUNC(("ENTER: mem:%p, arg:%p\n", mem, arg));
462 RT_NOREF(arg);
463 Assert(mem);
464 RTMemFree(mem);
465 NATMEM_LOG_FLOW_FUNC_LEAVE();
466}
467
468void *uma_zalloc(uma_zone_t zone, int len)
469{
470 NATMEM_LOG_FLOW_FUNC(("ENTER: zone:%R[mzone], len:%d\n", zone, len));
471 RT_NOREF(zone, len);
472 NATMEM_LOG_FLOW_FUNC(("LEAVE: NULL"));
473 return NULL;
474}
475
476struct mbuf *slirp_ext_m_get(PNATState pData, size_t cbMin, void **ppvBuf, size_t *pcbBuf)
477{
478 struct mbuf *m;
479 int size = MCLBYTES;
480 NATMEM_LOG_FLOW_FUNC(("ENTER: cbMin:%d, ppvBuf:%p, pcbBuf:%p\n", cbMin, ppvBuf, pcbBuf));
481
482 *ppvBuf = NULL;
483 *pcbBuf = 0;
484
485 if (cbMin < MCLBYTES)
486 size = MCLBYTES;
487 else if (cbMin < MJUM9BYTES)
488 size = MJUM9BYTES;
489 else if (cbMin < MJUM16BYTES)
490 size = MJUM16BYTES;
491 else
492 {
493 AssertMsgFailed(("Unsupported size %zu", cbMin));
494 NATMEM_LOG_FLOW_FUNC(("LEAVE: NULL (bad size %zu)\n", cbMin));
495 return NULL;
496 }
497
498 m = m_getjcl(pData, M_NOWAIT, MT_HEADER, M_PKTHDR, size);
499 if (m == NULL)
500 {
501 NATMEM_LOG_FLOW_FUNC(("LEAVE: NULL\n"));
502 return NULL;
503 }
504 m->m_len = size;
505 *ppvBuf = mtod(m, void *);
506 *pcbBuf = size;
507 NATMEM_LOG_FLOW_FUNC(("LEAVE: %p\n", m));
508 return m;
509}
510
511void slirp_ext_m_free(PNATState pData, struct mbuf *m, uint8_t *pu8Buf)
512{
513
514 NATMEM_LOG_FLOW_FUNC(("ENTER: m:%p, pu8Buf:%p\n", m, pu8Buf));
515 if ( !pu8Buf
516 && pu8Buf != mtod(m, uint8_t *))
517 RTMemFree(pu8Buf); /* This buffer was allocated on heap */
518 m_freem(pData, m);
519 NATMEM_LOG_FLOW_FUNC_LEAVE();
520}
521
522static void zone_destroy(uma_zone_t zone)
523{
524 RTCritSectEnter(&zone->csZone);
525 NATMEM_LOG_FLOW_FUNC(("ENTER: zone:%R[mzone]\n", zone));
526 LogRel(("NAT: Zone(nm:%s, used:%d)\n", zone->name, zone->cur_items));
527 RTMemFree(zone->area);
528 RTCritSectLeave(&zone->csZone);
529 RTCritSectDelete(&zone->csZone);
530 RTMemFree(zone);
531 NATMEM_LOG_FLOW_FUNC_LEAVE();
532}
533
534void m_fini(PNATState pData)
535{
536 NATMEM_LOG_FLOW_FUNC_ENTER();
537# define ZONE_DESTROY(zone) do { zone_destroy((zone)); (zone) = NULL;} while (0)
538 ZONE_DESTROY(pData->zone_clust);
539 ZONE_DESTROY(pData->zone_pack);
540 ZONE_DESTROY(pData->zone_mbuf);
541 ZONE_DESTROY(pData->zone_jumbop);
542 ZONE_DESTROY(pData->zone_jumbo9);
543 ZONE_DESTROY(pData->zone_jumbo16);
544 ZONE_DESTROY(pData->zone_ext_refcnt);
545# undef ZONE_DESTROY
546 /** @todo do finalize here.*/
547 NATMEM_LOG_FLOW_FUNC_LEAVE();
548}
549
550void
551if_init(PNATState pData)
552{
553 /* 14 for ethernet */
554 if_maxlinkhdr = 14;
555 if_comp = IF_AUTOCOMP;
556 if_mtu = 1500;
557 if_mru = 1500;
558}
559
560#endif /* VBOX_NAT_TST_QUEUE */
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