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