1 | /** @file
|
---|
2 | * INETNET - Internal Networking.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License as published by the Free Software Foundation,
|
---|
12 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
13 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
14 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * If you received this file as part of a commercial VirtualBox
|
---|
17 | * distribution, then only the terms of your commercial VirtualBox
|
---|
18 | * license agreement apply instead of the previous paragraph.
|
---|
19 | */
|
---|
20 |
|
---|
21 | #ifndef __VBox_intnet_h__
|
---|
22 | #define __VBox_intnet_h__
|
---|
23 |
|
---|
24 | #include <VBox/types.h>
|
---|
25 | #include <VBox/stam.h>
|
---|
26 | #include <iprt/assert.h>
|
---|
27 | #include <iprt/asm.h>
|
---|
28 |
|
---|
29 | __BEGIN_DECLS
|
---|
30 |
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Generic two-sided ring buffer.
|
---|
34 | *
|
---|
35 | * The deal is that there is exactly one writer and one reader.
|
---|
36 | * When offRead equals offWrite the buffer is empty. In the other
|
---|
37 | * extreme the writer will not use the last free byte in the buffer.
|
---|
38 | */
|
---|
39 | typedef struct INTNETRINGBUF
|
---|
40 | {
|
---|
41 | /** The start of the buffer offset relative to the. (inclusive) */
|
---|
42 | uint32_t offStart;
|
---|
43 | /** The offset to the end of the buffer. (exclusive) */
|
---|
44 | uint32_t offEnd;
|
---|
45 | /** The current read offset. */
|
---|
46 | uint32_t volatile offRead;
|
---|
47 | /** The current write offset. */
|
---|
48 | uint32_t volatile offWrite;
|
---|
49 | } INTNETRINGBUF;
|
---|
50 | /** Pointer to a ring buffer. */
|
---|
51 | typedef INTNETRINGBUF *PINTNETRINGBUF;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Get the amount of space available for writing.
|
---|
55 | *
|
---|
56 | * @returns Number of available bytes.
|
---|
57 | * @param pRingBuf The ring buffer.
|
---|
58 | */
|
---|
59 | DECLINLINE(uint32_t) INTNETRingGetWritable(PINTNETRINGBUF pRingBuf)
|
---|
60 | {
|
---|
61 | return pRingBuf->offRead <= pRingBuf->offWrite
|
---|
62 | ? pRingBuf->offEnd - pRingBuf->offWrite + pRingBuf->offRead - pRingBuf->offStart - 1
|
---|
63 | : pRingBuf->offRead - pRingBuf->offWrite - 1;
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Get the amount of data ready for reading.
|
---|
69 | *
|
---|
70 | * @returns Number of ready bytes.
|
---|
71 | * @param pRingBuf The ring buffer.
|
---|
72 | */
|
---|
73 | DECLINLINE(uint32_t) INTNETRingGetReadable(PINTNETRINGBUF pRingBuf)
|
---|
74 | {
|
---|
75 | return pRingBuf->offRead <= pRingBuf->offWrite
|
---|
76 | ? pRingBuf->offWrite - pRingBuf->offRead
|
---|
77 | : pRingBuf->offEnd - pRingBuf->offRead + pRingBuf->offWrite - pRingBuf->offStart;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * A interface buffer.
|
---|
83 | */
|
---|
84 | typedef struct INTNETBUF
|
---|
85 | {
|
---|
86 | /** The size of the entire buffer. */
|
---|
87 | uint32_t cbBuf;
|
---|
88 | /** The size of the send area. */
|
---|
89 | uint32_t cbSend;
|
---|
90 | /** The size of the receive area. */
|
---|
91 | uint32_t cbRecv;
|
---|
92 | /** The receive buffer. */
|
---|
93 | INTNETRINGBUF Recv;
|
---|
94 | /** The send buffer. */
|
---|
95 | INTNETRINGBUF Send;
|
---|
96 | /** Number of times yields help solve an overflow. */
|
---|
97 | STAMCOUNTER cStatYieldsOk;
|
---|
98 | /** Number of times yields didn't help solve an overflow. */
|
---|
99 | STAMCOUNTER cStatYieldsNok;
|
---|
100 | /** Number of lost packets due to overflows. */
|
---|
101 | STAMCOUNTER cStatLost;
|
---|
102 | /** Number of packets received (not counting lost ones). */
|
---|
103 | STAMCOUNTER cStatRecvs;
|
---|
104 | /** Number of frame bytes received (not couting lost frames). */
|
---|
105 | STAMCOUNTER cbStatRecv;
|
---|
106 | /** Number of packets received. */
|
---|
107 | STAMCOUNTER cStatSends;
|
---|
108 | /** Number of frame bytes sent. */
|
---|
109 | STAMCOUNTER cbStatSend;
|
---|
110 | } INTNETBUF;
|
---|
111 | typedef INTNETBUF *PINTNETBUF;
|
---|
112 |
|
---|
113 | /** Internal networking interface handle. */
|
---|
114 | typedef uint32_t INTNETIFHANDLE;
|
---|
115 | /** Pointer to an internal networking interface handle. */
|
---|
116 | typedef INTNETIFHANDLE *PINTNETIFHANDLE;
|
---|
117 |
|
---|
118 | /** Or mask to obscure the handle index. */
|
---|
119 | #define INTNET_HANDLE_MAGIC 0x88880000
|
---|
120 | /** Mask to extract the handle index. */
|
---|
121 | #define INTNET_HANDLE_INDEX_MASK 0xffff
|
---|
122 | /** The maximum number of handles (exclusive) */
|
---|
123 | #define INTNET_HANDLE_MAX 0xffff
|
---|
124 | /** Invalid handle. */
|
---|
125 | #define INTNET_HANDLE_INVALID (0)
|
---|
126 |
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * The packet header.
|
---|
130 | *
|
---|
131 | * The header is intentionally 8 bytes long. It will always
|
---|
132 | * start at an 8 byte aligned address. Assuming that the buffer
|
---|
133 | * size is a multiple of 8 bytes, that means that we can guarantee
|
---|
134 | * that the entire header is contiguous in both virtual and physical
|
---|
135 | * memory.
|
---|
136 | */
|
---|
137 | #pragma pack(1)
|
---|
138 | typedef struct INTNETHDR
|
---|
139 | {
|
---|
140 | /** Header type. This is currently serving as a magic, it
|
---|
141 | * can be extended later to encode special command packets and stuff.. */
|
---|
142 | uint16_t u16Type;
|
---|
143 | /** The size of the frame. */
|
---|
144 | uint16_t cbFrame;
|
---|
145 | /** The offset from the start of this header to where the actual frame starts.
|
---|
146 | * This is used to keep the frame it self continguous in virtual memory and
|
---|
147 | * thereby both simplify reading and */
|
---|
148 | int32_t offFrame;
|
---|
149 | } INTNETHDR, *PINTNETHDR;
|
---|
150 | #pragma pack()
|
---|
151 |
|
---|
152 | /** INTNETHDR::u16Type value for normal frames. */
|
---|
153 | #define INTNETHDR_TYPE_FRAME 0x2442
|
---|
154 |
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Calculates the pointer to the frame.
|
---|
158 | *
|
---|
159 | * @returns Pointer to the start of the frame.
|
---|
160 | * @param pHdr Pointer to the packet header
|
---|
161 | * @param pBuf The buffer the header is within. Only used in strict builds.
|
---|
162 | */
|
---|
163 | DECLINLINE(void *) INTNETHdrGetFramePtr(PINTNETHDR pHdr, PINTNETBUF pBuf)
|
---|
164 | {
|
---|
165 | uint8_t *pu8 = (uint8_t *)pHdr + pHdr->offFrame;
|
---|
166 | #ifdef VBOX_STRICT
|
---|
167 | const uintptr_t off = (uintptr_t)pu8 - (uintptr_t)pBuf;
|
---|
168 | Assert(pHdr->u16Type == INTNETHDR_TYPE_FRAME);
|
---|
169 | Assert(off < pBuf->cbBuf);
|
---|
170 | Assert(off + pHdr->cbFrame < pBuf->cbBuf);
|
---|
171 | #endif
|
---|
172 | NOREF(pBuf);
|
---|
173 | return pu8;
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Skips to the next (read) frame in the buffer.
|
---|
179 | *
|
---|
180 | * @param pBuf The buffer.
|
---|
181 | * @param pRingBuf The ring buffer in question.
|
---|
182 | */
|
---|
183 | DECLINLINE(void) INTNETRingSkipFrame(PINTNETBUF pBuf, PINTNETRINGBUF pRingBuf)
|
---|
184 | {
|
---|
185 | Assert(pRingBuf->offRead < pBuf->cbBuf);
|
---|
186 | Assert(pRingBuf->offRead >= pRingBuf->offStart);
|
---|
187 | Assert(pRingBuf->offRead < pRingBuf->offEnd);
|
---|
188 | uint32_t offRead = pRingBuf->offRead;
|
---|
189 | PINTNETHDR pHdr = (PINTNETHDR)((uint8_t *)pBuf + offRead);
|
---|
190 |
|
---|
191 | /* skip the frame */
|
---|
192 | offRead += pHdr->offFrame + pHdr->cbFrame;
|
---|
193 | offRead = RT_ALIGN_32(offRead, sizeof(INTNETHDR));
|
---|
194 | Assert(offRead <= pRingBuf->offEnd && offRead >= pRingBuf->offStart);
|
---|
195 | if (offRead >= pRingBuf->offEnd)
|
---|
196 | offRead = pRingBuf->offStart;
|
---|
197 | ASMAtomicXchgU32(&pRingBuf->offRead, offRead);
|
---|
198 | }
|
---|
199 |
|
---|
200 | /** The maximum length of a network name. */
|
---|
201 | #define INTNET_MAX_NETWORK_NAME 128
|
---|
202 |
|
---|
203 |
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * The packed down arguments of INTNETR0Open().
|
---|
207 | * @see INTNETR0Open()
|
---|
208 | */
|
---|
209 | typedef struct INTNETOPENARGS
|
---|
210 | {
|
---|
211 | /** The network name. (input) */
|
---|
212 | char szNetwork[INTNET_MAX_NETWORK_NAME];
|
---|
213 | /** The size of the send buffer. (input) */
|
---|
214 | uint32_t cbSend;
|
---|
215 | /** The size of the receive buffer. (input) */
|
---|
216 | uint32_t cbRecv;
|
---|
217 | /** The handle to the network interface. (output) */
|
---|
218 | INTNETIFHANDLE hIf;
|
---|
219 | } INTNETOPENARGS;
|
---|
220 | /** Pointer to an INTNETR0Open() argument package. */
|
---|
221 | typedef INTNETOPENARGS *PINTNETOPENARGS;
|
---|
222 |
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * The packed down arguments of INTNETR0IfClose().
|
---|
226 | * @see INTNETR0IfClose()
|
---|
227 | */
|
---|
228 | typedef struct INTNETCLOSEARGS
|
---|
229 | {
|
---|
230 | /** The handle to the network interface. */
|
---|
231 | INTNETIFHANDLE hIf;
|
---|
232 | } INTNETIFCLOSEARGS;
|
---|
233 | /** Pointer to an INTNETR0Open() argument package. */
|
---|
234 | typedef INTNETIFCLOSEARGS *PINTNETIFCLOSEARGS;
|
---|
235 |
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * Argument buffer for calling INTNETR0IfGetRing3Buffer().
|
---|
239 | * @see INTNETR0IfGetRing3Buffer()
|
---|
240 | */
|
---|
241 | typedef struct INTNETIFGETRING3BUFFERARGS
|
---|
242 | {
|
---|
243 | /** Handle to the interface. */
|
---|
244 | INTNETIFHANDLE hIf;
|
---|
245 | /** The pointer to the ring3 buffer. (output) */
|
---|
246 | PINTNETBUF pRing3Buf;
|
---|
247 | } INTNETIFGETRING3BUFFERARGS;
|
---|
248 | /** Pointer to an INTNETR0IfGetRing3Buffer() argument package. */
|
---|
249 | typedef INTNETIFGETRING3BUFFERARGS *PINTNETIFGETRING3BUFFERARGS;
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * Argument buffer for calling INTNETR0IfSetPromiscuousMode().
|
---|
253 | * @see INTNETR0IfSetPromiscuousMode()
|
---|
254 | */
|
---|
255 | typedef struct INTNETIFSETPROMISCUOUSMODEARGS
|
---|
256 | {
|
---|
257 | /** Handle to the interface. */
|
---|
258 | INTNETIFHANDLE hIf;
|
---|
259 | /** The new promiscuous mode. */
|
---|
260 | bool fPromiscuous;
|
---|
261 | } INTNETIFSETPROMISCUOUSMODEARGS;
|
---|
262 | /** Pointer to an INTNETR0IfSetPromiscuousMode() argument package. */
|
---|
263 | typedef INTNETIFSETPROMISCUOUSMODEARGS *PINTNETIFSETPROMISCUOUSMODEARGS;
|
---|
264 |
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * Argument buffer for calling INTNETR0IfSend().
|
---|
268 | * @see INTNETR0IfSend()
|
---|
269 | */
|
---|
270 | typedef struct INTNETIFSENDARGS
|
---|
271 | {
|
---|
272 | /** Handle to the interface. */
|
---|
273 | INTNETIFHANDLE hIf;
|
---|
274 | /** Pointer to the frame. (Optional) */
|
---|
275 | const void *pvFrame;
|
---|
276 | /** The size of the frame. (Optional) */
|
---|
277 | uint32_t cbFrame;
|
---|
278 | } INTNETIFSENDARGS;
|
---|
279 | /** Pointer to an INTNETR0IfSend() argument package. */
|
---|
280 | typedef INTNETIFSENDARGS *PINTNETIFSENDARGS;
|
---|
281 |
|
---|
282 |
|
---|
283 | /**
|
---|
284 | * Argument buffer for calling INTNETR0IfWait().
|
---|
285 | * @see INTNETR0IfWait()
|
---|
286 | */
|
---|
287 | typedef struct INTNETIFWAITARGS
|
---|
288 | {
|
---|
289 | /** Handle to the interface. */
|
---|
290 | INTNETIFHANDLE hIf;
|
---|
291 | /** The number of milliseconds to wait. */
|
---|
292 | unsigned cMillies;
|
---|
293 | } INTNETSENDARGS;
|
---|
294 | /** Pointer to an INTNETR0IfWait() argument package. */
|
---|
295 | typedef INTNETIFWAITARGS *PINTNETIFWAITARGS;
|
---|
296 |
|
---|
297 |
|
---|
298 | #if defined(IN_RING0) || defined(IN_INTNET_TESTCASE)
|
---|
299 | /** @name
|
---|
300 | * @{
|
---|
301 | */
|
---|
302 |
|
---|
303 | /** Pointer to an internal network ring-0 instance. */
|
---|
304 | typedef struct INTNET *PINTNET;
|
---|
305 |
|
---|
306 | /**
|
---|
307 | * Create an instance of the Ring-0 internal networking service.
|
---|
308 | *
|
---|
309 | * @returns VBox status code.
|
---|
310 | * @param ppIntNet Where to store the instance pointer.
|
---|
311 | */
|
---|
312 | INTNETR0DECL(int) INTNETR0Create(PINTNET *ppIntNet);
|
---|
313 |
|
---|
314 | /**
|
---|
315 | * Destroys an instance of the Ring-0 internal networking service.
|
---|
316 | *
|
---|
317 | * @param pIntNet Pointer to the instance data.
|
---|
318 | */
|
---|
319 | INTNETR0DECL(void) INTNETR0Destroy(PINTNET pIntNet);
|
---|
320 |
|
---|
321 | /**
|
---|
322 | * Opens a network interface and attaches it to the specified network.
|
---|
323 | *
|
---|
324 | * @returns VBox status code.
|
---|
325 | * @param pIntNet The
|
---|
326 | */
|
---|
327 | INTNETR0DECL(int) INTNETR0Open(PINTNET pIntNet, PSUPDRVSESSION pSession, const char *pszNetwork, unsigned cbSend, unsigned cbRecv, PINTNETIFHANDLE phIf);
|
---|
328 |
|
---|
329 | /**
|
---|
330 | * Close an interface.
|
---|
331 | *
|
---|
332 | * @returns VBox status code.
|
---|
333 | * @param pIntNet The instance handle.
|
---|
334 | * @param hIf The interface handle.
|
---|
335 | */
|
---|
336 | INTNETR0DECL(int) INTNETR0IfClose(PINTNET pIntNet, INTNETIFHANDLE hIf);
|
---|
337 |
|
---|
338 | /**
|
---|
339 | * Gets the ring-0 address of the current buffer.
|
---|
340 | *
|
---|
341 | * @returns VBox status code.
|
---|
342 | * @param pIntNet The instance data.
|
---|
343 | * @param hIF The interface handle.
|
---|
344 | * @param ppRing0Buf Where to store the address of the ring-3 mapping.
|
---|
345 | */
|
---|
346 | INTNETR0DECL(int) INTNETR0IfGetRing0Buffer(PINTNET pIntNet, INTNETIFHANDLE hIf, PINTNETBUF *ppRing0Buf);
|
---|
347 |
|
---|
348 | /**
|
---|
349 | * Maps the default buffer into ring 3.
|
---|
350 | *
|
---|
351 | * @returns VBox status code.
|
---|
352 | * @param pIntNet The instance data.
|
---|
353 | * @param hIF The interface handle.
|
---|
354 | * @param ppRing3Buf Where to store the address of the ring-3 mapping.
|
---|
355 | */
|
---|
356 | INTNETR0DECL(int) INTNETR0IfGetRing3Buffer(PINTNET pIntNet, INTNETIFHANDLE hIf, PINTNETBUF *ppRing3Buf);
|
---|
357 |
|
---|
358 | /**
|
---|
359 | * Sets the promiscuous mode property of an interface.
|
---|
360 | *
|
---|
361 | * @returns VBox status code.
|
---|
362 | * @param pIntNet The instance handle.
|
---|
363 | * @param hIf The interface handle.
|
---|
364 | * @param fPromiscuous Set if the interface should be in promiscuous mode, clear if not.
|
---|
365 | */
|
---|
366 | INTNETR0DECL(int) INTNETR0IfSetPromiscuousMode(PINTNET pIntNet, INTNETIFHANDLE hIf, bool fPromiscuous);
|
---|
367 |
|
---|
368 | /**
|
---|
369 | * Sends one or more frames.
|
---|
370 | *
|
---|
371 | * The function will first the frame which is passed as the optional
|
---|
372 | * arguments pvFrame and cbFrame. These are optional since it also
|
---|
373 | * possible to chain together one or more frames in the send buffer
|
---|
374 | * which the function will process after considering it's arguments.
|
---|
375 | *
|
---|
376 | * @returns VBox status code.
|
---|
377 | * @param pIntNet The instance data.
|
---|
378 | * @param hIF The interface handle.
|
---|
379 | * @param pvFrame Pointer to the frame.
|
---|
380 | * @param cbFrame Size of the frame.
|
---|
381 | */
|
---|
382 | INTNETR0DECL(int) INTNETR0IfSend(PINTNET pIntNet, INTNETIFHANDLE hIf, const void *pvFrame, unsigned cbFrame);
|
---|
383 |
|
---|
384 | /**
|
---|
385 | * Wait for the interface to get signaled.
|
---|
386 | * The interface will be signaled when is put into the receive buffer.
|
---|
387 | *
|
---|
388 | * @returns VBox status code.
|
---|
389 | * @param pIntNet The instance handle.
|
---|
390 | * @param hIf The interface handle.
|
---|
391 | * @param cMillies Number of milliseconds to wait. RT_INDEFINITE_WAIT should be
|
---|
392 | * used if indefinite wait is desired.
|
---|
393 | */
|
---|
394 | INTNETR0DECL(int) INTNETR0IfWait(PINTNET pIntNet, INTNETIFHANDLE hIf, unsigned cMillies);
|
---|
395 |
|
---|
396 | /** @} */
|
---|
397 | #endif /* IN_RING0 */
|
---|
398 |
|
---|
399 | __END_DECLS
|
---|
400 |
|
---|
401 | #endif
|
---|