1 | /* $Id: IntNetIf.cpp 102797 2024-01-09 15:01:16Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IntNetIfCtx - Abstract API implementing an IntNet connection using the R0 support driver or some R3 IPC variant.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2022-2023 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 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #if defined(VBOX_WITH_INTNET_SERVICE_IN_R3)
|
---|
33 | # if defined(RT_OS_DARWIN)
|
---|
34 | # include <xpc/xpc.h> /* This needs to be here because it drags PVM in and cdefs.h needs to undefine it... */
|
---|
35 | # else
|
---|
36 | # error "R3 internal networking not implemented for this platform yet!"
|
---|
37 | # endif
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | #include <iprt/cdefs.h>
|
---|
41 | #include <iprt/path.h>
|
---|
42 | #include <iprt/semaphore.h>
|
---|
43 |
|
---|
44 | #include <VBox/err.h>
|
---|
45 | #include <VBox/sup.h>
|
---|
46 | #include <VBox/intnetinline.h>
|
---|
47 | #include <VBox/vmm/pdmnetinline.h>
|
---|
48 |
|
---|
49 | #include "IntNetIf.h"
|
---|
50 |
|
---|
51 |
|
---|
52 | /*********************************************************************************************************************************
|
---|
53 | * Defined Constants And Macros *
|
---|
54 | *********************************************************************************************************************************/
|
---|
55 |
|
---|
56 |
|
---|
57 | /*********************************************************************************************************************************
|
---|
58 | * Structures and Typedefs *
|
---|
59 | *********************************************************************************************************************************/
|
---|
60 |
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Internal network interface context instance data.
|
---|
64 | */
|
---|
65 | typedef struct INTNETIFCTXINT
|
---|
66 | {
|
---|
67 | /** The support driver session handle. */
|
---|
68 | PSUPDRVSESSION pSupDrvSession;
|
---|
69 | /** Interface handle. */
|
---|
70 | INTNETIFHANDLE hIf;
|
---|
71 | /** The internal network buffer. */
|
---|
72 | PINTNETBUF pBuf;
|
---|
73 | #if defined (VBOX_WITH_INTNET_SERVICE_IN_R3)
|
---|
74 | /** Flag whether this interface is using the internal network switch in userspace path. */
|
---|
75 | bool fIntNetR3Svc;
|
---|
76 | /** Receive event semaphore. */
|
---|
77 | RTSEMEVENT hEvtRecv;
|
---|
78 | # if defined(RT_OS_DARWIN)
|
---|
79 | /** XPC connection handle to the R3 internal network switch service. */
|
---|
80 | xpc_connection_t hXpcCon;
|
---|
81 | /** Size of the communication buffer in bytes. */
|
---|
82 | size_t cbBuf;
|
---|
83 | # endif
|
---|
84 | #endif
|
---|
85 | } INTNETIFCTXINT;
|
---|
86 | /** Pointer to the internal network interface context instance data. */
|
---|
87 | typedef INTNETIFCTXINT *PINTNETIFCTXINT;
|
---|
88 |
|
---|
89 |
|
---|
90 | /*********************************************************************************************************************************
|
---|
91 | * Internal Functions *
|
---|
92 | *********************************************************************************************************************************/
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Calls the internal networking switch service living in either R0 or in another R3 process.
|
---|
96 | *
|
---|
97 | * @returns VBox status code.
|
---|
98 | * @param pThis The internal network driver instance data.
|
---|
99 | * @param uOperation The operation to execute.
|
---|
100 | * @param pReqHdr Pointer to the request header.
|
---|
101 | */
|
---|
102 | static int intnetR3IfCallSvc(PINTNETIFCTXINT pThis, uint32_t uOperation, PSUPVMMR0REQHDR pReqHdr)
|
---|
103 | {
|
---|
104 | #if defined(VBOX_WITH_INTNET_SERVICE_IN_R3)
|
---|
105 | if (pThis->fIntNetR3Svc)
|
---|
106 | {
|
---|
107 | # if defined(RT_OS_DARWIN)
|
---|
108 | size_t cbReq = pReqHdr->cbReq;
|
---|
109 | xpc_object_t hObj = xpc_dictionary_create(NULL, NULL, 0);
|
---|
110 | xpc_dictionary_set_uint64(hObj, "req-id", uOperation);
|
---|
111 | xpc_dictionary_set_data(hObj, "req", pReqHdr, pReqHdr->cbReq);
|
---|
112 | xpc_object_t hObjReply = xpc_connection_send_message_with_reply_sync(pThis->hXpcCon, hObj);
|
---|
113 | xpc_release(hObj);
|
---|
114 |
|
---|
115 | int rc = (int)xpc_dictionary_get_int64(hObjReply, "rc");
|
---|
116 |
|
---|
117 | size_t cbReply = 0;
|
---|
118 | const void *pvData = xpc_dictionary_get_data(hObjReply, "reply", &cbReply);
|
---|
119 | AssertRelease(cbReply == cbReq);
|
---|
120 | memcpy(pReqHdr, pvData, cbReq);
|
---|
121 | xpc_release(hObjReply);
|
---|
122 |
|
---|
123 | return rc;
|
---|
124 | # endif
|
---|
125 | }
|
---|
126 | else
|
---|
127 | #else
|
---|
128 | RT_NOREF(pThis);
|
---|
129 | #endif
|
---|
130 | return SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, uOperation, 0, pReqHdr);
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | #if defined(RT_OS_DARWIN) && defined(VBOX_WITH_INTNET_SERVICE_IN_R3)
|
---|
135 | /**
|
---|
136 | * Calls the internal networking switch service living in either R0 or in another R3 process.
|
---|
137 | *
|
---|
138 | * @returns VBox status code.
|
---|
139 | * @param pThis The internal network driver instance data.
|
---|
140 | * @param uOperation The operation to execute.
|
---|
141 | * @param pReqHdr Pointer to the request header.
|
---|
142 | */
|
---|
143 | static int intnetR3IfCallSvcAsync(PINTNETIFCTXINT pThis, uint32_t uOperation, PSUPVMMR0REQHDR pReqHdr)
|
---|
144 | {
|
---|
145 | if (pThis->fIntNetR3Svc)
|
---|
146 | {
|
---|
147 | xpc_object_t hObj = xpc_dictionary_create(NULL, NULL, 0);
|
---|
148 | xpc_dictionary_set_uint64(hObj, "req-id", uOperation);
|
---|
149 | xpc_dictionary_set_data(hObj, "req", pReqHdr, pReqHdr->cbReq);
|
---|
150 | xpc_connection_send_message(pThis->hXpcCon, hObj);
|
---|
151 | return VINF_SUCCESS;
|
---|
152 | }
|
---|
153 | else
|
---|
154 | return SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, uOperation, 0, pReqHdr);
|
---|
155 | }
|
---|
156 | #endif
|
---|
157 |
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Map the ring buffer pointer into this process R3 address space.
|
---|
161 | *
|
---|
162 | * @returns VBox status code.
|
---|
163 | * @param pThis The internal network driver instance data.
|
---|
164 | */
|
---|
165 | static int intnetR3IfMapBufferPointers(PINTNETIFCTXINT pThis)
|
---|
166 | {
|
---|
167 | int rc = VINF_SUCCESS;
|
---|
168 |
|
---|
169 | INTNETIFGETBUFFERPTRSREQ GetBufferPtrsReq;
|
---|
170 | GetBufferPtrsReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
171 | GetBufferPtrsReq.Hdr.cbReq = sizeof(GetBufferPtrsReq);
|
---|
172 | GetBufferPtrsReq.pSession = pThis->pSupDrvSession;
|
---|
173 | GetBufferPtrsReq.hIf = pThis->hIf;
|
---|
174 | GetBufferPtrsReq.pRing3Buf = NULL;
|
---|
175 | GetBufferPtrsReq.pRing0Buf = NIL_RTR0PTR;
|
---|
176 |
|
---|
177 | #if defined(VBOX_WITH_INTNET_SERVICE_IN_R3)
|
---|
178 | if (pThis->fIntNetR3Svc)
|
---|
179 | {
|
---|
180 | #if defined(RT_OS_DARWIN)
|
---|
181 | xpc_object_t hObj = xpc_dictionary_create(NULL, NULL, 0);
|
---|
182 | xpc_dictionary_set_uint64(hObj, "req-id", VMMR0_DO_INTNET_IF_GET_BUFFER_PTRS);
|
---|
183 | xpc_dictionary_set_data(hObj, "req", &GetBufferPtrsReq, sizeof(GetBufferPtrsReq));
|
---|
184 | xpc_object_t hObjReply = xpc_connection_send_message_with_reply_sync(pThis->hXpcCon, hObj);
|
---|
185 | xpc_release(hObj);
|
---|
186 |
|
---|
187 | rc = (int)xpc_dictionary_get_int64(hObjReply, "rc");
|
---|
188 | if (RT_SUCCESS(rc))
|
---|
189 | {
|
---|
190 | /* Get the shared memory object. */
|
---|
191 | xpc_object_t hObjShMem = xpc_dictionary_get_value(hObjReply, "buf-ptr");
|
---|
192 | size_t cbMem = xpc_shmem_map(hObjShMem, (void **)&pThis->pBuf);
|
---|
193 | if (!cbMem)
|
---|
194 | rc = VERR_NO_MEMORY;
|
---|
195 | else
|
---|
196 | pThis->cbBuf = cbMem;
|
---|
197 | }
|
---|
198 | xpc_release(hObjReply);
|
---|
199 | #endif
|
---|
200 | }
|
---|
201 | else
|
---|
202 | #endif
|
---|
203 | {
|
---|
204 | rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_INTNET_IF_GET_BUFFER_PTRS, 0 /*u64Arg*/, &GetBufferPtrsReq.Hdr);
|
---|
205 | if (RT_SUCCESS(rc))
|
---|
206 | {
|
---|
207 | AssertRelease(RT_VALID_PTR(GetBufferPtrsReq.pRing3Buf));
|
---|
208 | pThis->pBuf = GetBufferPtrsReq.pRing3Buf;
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | return rc;
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 | static void intnetR3IfClose(PINTNETIFCTXINT pThis)
|
---|
217 | {
|
---|
218 | if (pThis->hIf != INTNET_HANDLE_INVALID)
|
---|
219 | {
|
---|
220 | INTNETIFCLOSEREQ CloseReq;
|
---|
221 | CloseReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
222 | CloseReq.Hdr.cbReq = sizeof(CloseReq);
|
---|
223 | CloseReq.pSession = pThis->pSupDrvSession;
|
---|
224 | CloseReq.hIf = pThis->hIf;
|
---|
225 |
|
---|
226 | pThis->hIf = INTNET_HANDLE_INVALID;
|
---|
227 | int rc = intnetR3IfCallSvc(pThis, VMMR0_DO_INTNET_IF_CLOSE, &CloseReq.Hdr);
|
---|
228 | AssertRC(rc);
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 | DECLHIDDEN(int) IntNetR3IfCreate(PINTNETIFCTX phIfCtx, const char *pszNetwork)
|
---|
234 | {
|
---|
235 | return IntNetR3IfCreateEx(phIfCtx, pszNetwork, kIntNetTrunkType_WhateverNone, "",
|
---|
236 | _128K /*cbSend*/, _256K /*cbRecv*/, 0 /*fFlags*/);
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | DECLHIDDEN(int) IntNetR3IfCreateEx(PINTNETIFCTX phIfCtx, const char *pszNetwork, INTNETTRUNKTYPE enmTrunkType,
|
---|
241 | const char *pszTrunk, uint32_t cbSend, uint32_t cbRecv, uint32_t fFlags)
|
---|
242 | {
|
---|
243 | AssertPtrReturn(phIfCtx, VERR_INVALID_POINTER);
|
---|
244 | AssertPtrReturn(pszNetwork, VERR_INVALID_POINTER);
|
---|
245 | AssertPtrReturn(pszTrunk, VERR_INVALID_POINTER);
|
---|
246 |
|
---|
247 | PSUPDRVSESSION pSession = NIL_RTR0PTR;
|
---|
248 | int rc = SUPR3Init(&pSession);
|
---|
249 | if (RT_SUCCESS(rc))
|
---|
250 | {
|
---|
251 | PINTNETIFCTXINT pThis = (PINTNETIFCTXINT)RTMemAllocZ(sizeof(*pThis));
|
---|
252 | if (RT_LIKELY(pThis))
|
---|
253 | {
|
---|
254 | pThis->pSupDrvSession = pSession;
|
---|
255 | #if defined(VBOX_WITH_INTNET_SERVICE_IN_R3)
|
---|
256 | pThis->hEvtRecv = NIL_RTSEMEVENT;
|
---|
257 | #endif
|
---|
258 |
|
---|
259 | /* Driverless operation needs support for running the internal network switch using IPC. */
|
---|
260 | if (SUPR3IsDriverless())
|
---|
261 | {
|
---|
262 | #if defined(VBOX_WITH_INTNET_SERVICE_IN_R3)
|
---|
263 | # if defined(RT_OS_DARWIN)
|
---|
264 | xpc_connection_t hXpcCon = xpc_connection_create(INTNET_R3_SVC_NAME, NULL);
|
---|
265 | xpc_connection_set_event_handler(hXpcCon, ^(xpc_object_t hObj) {
|
---|
266 | if (xpc_get_type(hObj) == XPC_TYPE_ERROR)
|
---|
267 | {
|
---|
268 | /** @todo Error handling - reconnecting. */
|
---|
269 | }
|
---|
270 | else
|
---|
271 | {
|
---|
272 | /* Out of band messages should only come when there is something to receive. */
|
---|
273 | RTSemEventSignal(pThis->hEvtRecv);
|
---|
274 | }
|
---|
275 | });
|
---|
276 |
|
---|
277 | xpc_connection_resume(hXpcCon);
|
---|
278 | pThis->hXpcCon = hXpcCon;
|
---|
279 | # endif
|
---|
280 | pThis->fIntNetR3Svc = true;
|
---|
281 | rc = RTSemEventCreate(&pThis->hEvtRecv);
|
---|
282 | #else
|
---|
283 | rc = VERR_SUP_DRIVERLESS;
|
---|
284 | #endif
|
---|
285 | }
|
---|
286 | else
|
---|
287 | {
|
---|
288 | /* Need to load VMMR0.r0 containing the network switching code. */
|
---|
289 | char szPathVMMR0[RTPATH_MAX];
|
---|
290 |
|
---|
291 | rc = RTPathExecDir(szPathVMMR0, sizeof(szPathVMMR0));
|
---|
292 | if (RT_SUCCESS(rc))
|
---|
293 | {
|
---|
294 | rc = RTPathAppend(szPathVMMR0, sizeof(szPathVMMR0), "VMMR0.r0");
|
---|
295 | if (RT_SUCCESS(rc))
|
---|
296 | rc = SUPR3LoadVMM(szPathVMMR0, /* :pErrInfo */ NULL);
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | if (RT_SUCCESS(rc))
|
---|
301 | {
|
---|
302 | /* Open the interface. */
|
---|
303 | INTNETOPENREQ OpenReq;
|
---|
304 | RT_ZERO(OpenReq);
|
---|
305 |
|
---|
306 | OpenReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
307 | OpenReq.Hdr.cbReq = sizeof(OpenReq);
|
---|
308 | OpenReq.pSession = pThis->pSupDrvSession;
|
---|
309 | OpenReq.enmTrunkType = enmTrunkType;
|
---|
310 | OpenReq.fFlags = fFlags;
|
---|
311 | OpenReq.cbSend = cbSend;
|
---|
312 | OpenReq.cbRecv = cbRecv;
|
---|
313 | OpenReq.hIf = INTNET_HANDLE_INVALID;
|
---|
314 |
|
---|
315 | rc = RTStrCopy(OpenReq.szNetwork, sizeof(OpenReq.szNetwork), pszNetwork);
|
---|
316 | if (RT_SUCCESS(rc))
|
---|
317 | rc = RTStrCopy(OpenReq.szTrunk, sizeof(OpenReq.szTrunk), pszTrunk);
|
---|
318 | if (RT_SUCCESS(rc))
|
---|
319 | {
|
---|
320 | rc = intnetR3IfCallSvc(pThis, VMMR0_DO_INTNET_OPEN, &OpenReq.Hdr);
|
---|
321 | if (RT_SUCCESS(rc))
|
---|
322 | {
|
---|
323 | pThis->hIf = OpenReq.hIf;
|
---|
324 |
|
---|
325 | rc = intnetR3IfMapBufferPointers(pThis);
|
---|
326 | if (RT_SUCCESS(rc))
|
---|
327 | {
|
---|
328 | *phIfCtx = pThis;
|
---|
329 | return VINF_SUCCESS;
|
---|
330 | }
|
---|
331 | }
|
---|
332 |
|
---|
333 | intnetR3IfClose(pThis);
|
---|
334 | }
|
---|
335 | }
|
---|
336 |
|
---|
337 | #if defined(VBOX_WITH_INTNET_SERVICE_IN_R3)
|
---|
338 | if (pThis->fIntNetR3Svc)
|
---|
339 | {
|
---|
340 | # if defined(RT_OS_DARWIN)
|
---|
341 | if (pThis->hXpcCon)
|
---|
342 | xpc_connection_cancel(pThis->hXpcCon);
|
---|
343 | pThis->hXpcCon = NULL;
|
---|
344 | # endif
|
---|
345 |
|
---|
346 | if (pThis->hEvtRecv != NIL_RTSEMEVENT)
|
---|
347 | RTSemEventDestroy(pThis->hEvtRecv);
|
---|
348 | }
|
---|
349 | #endif
|
---|
350 |
|
---|
351 | RTMemFree(pThis);
|
---|
352 | }
|
---|
353 |
|
---|
354 | SUPR3Term();
|
---|
355 | }
|
---|
356 |
|
---|
357 | return rc;
|
---|
358 | }
|
---|
359 |
|
---|
360 |
|
---|
361 | DECLHIDDEN(int) IntNetR3IfDestroy(INTNETIFCTX hIfCtx)
|
---|
362 | {
|
---|
363 | PINTNETIFCTXINT pThis = hIfCtx;
|
---|
364 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
365 |
|
---|
366 | intnetR3IfClose(pThis);
|
---|
367 |
|
---|
368 | #if defined(VBOX_WITH_INTNET_SERVICE_IN_R3)
|
---|
369 | if (pThis->fIntNetR3Svc)
|
---|
370 | {
|
---|
371 | # if defined(RT_OS_DARWIN)
|
---|
372 | /* Unmap the shared buffer. */
|
---|
373 | munmap(pThis->pBuf, pThis->cbBuf);
|
---|
374 | xpc_connection_cancel(pThis->hXpcCon);
|
---|
375 | pThis->hXpcCon = NULL;
|
---|
376 | # endif
|
---|
377 | RTSemEventDestroy(pThis->hEvtRecv);
|
---|
378 | pThis->fIntNetR3Svc = false;
|
---|
379 | }
|
---|
380 | #endif
|
---|
381 |
|
---|
382 | RTMemFree(pThis);
|
---|
383 | return VINF_SUCCESS;
|
---|
384 | }
|
---|
385 |
|
---|
386 |
|
---|
387 | DECLHIDDEN(int) IntNetR3IfQueryBufferPtr(INTNETIFCTX hIfCtx, PINTNETBUF *ppIfBuf)
|
---|
388 | {
|
---|
389 | PINTNETIFCTXINT pThis = hIfCtx;
|
---|
390 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
391 | AssertPtrReturn(ppIfBuf, VERR_INVALID_POINTER);
|
---|
392 |
|
---|
393 | *ppIfBuf = pThis->pBuf;
|
---|
394 | return VINF_SUCCESS;
|
---|
395 | }
|
---|
396 |
|
---|
397 |
|
---|
398 | DECLHIDDEN(int) IntNetR3IfSetActive(INTNETIFCTX hIfCtx, bool fActive)
|
---|
399 | {
|
---|
400 | PINTNETIFCTXINT pThis = hIfCtx;
|
---|
401 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
402 |
|
---|
403 | INTNETIFSETACTIVEREQ Req;
|
---|
404 | Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
405 | Req.Hdr.cbReq = sizeof(Req);
|
---|
406 | Req.pSession = pThis->pSupDrvSession;
|
---|
407 | Req.hIf = pThis->hIf;
|
---|
408 | Req.fActive = fActive;
|
---|
409 | return intnetR3IfCallSvc(pThis, VMMR0_DO_INTNET_IF_SET_ACTIVE, &Req.Hdr);
|
---|
410 | }
|
---|
411 |
|
---|
412 |
|
---|
413 | DECLHIDDEN(int) IntNetR3IfSetPromiscuous(INTNETIFCTX hIfCtx, bool fPromiscuous)
|
---|
414 | {
|
---|
415 | PINTNETIFCTXINT pThis = hIfCtx;
|
---|
416 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
417 |
|
---|
418 | INTNETIFSETPROMISCUOUSMODEREQ Req;
|
---|
419 | Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
420 | Req.Hdr.cbReq = sizeof(Req);
|
---|
421 | Req.pSession = pThis->pSupDrvSession;
|
---|
422 | Req.hIf = pThis->hIf;
|
---|
423 | Req.fPromiscuous = fPromiscuous;
|
---|
424 | return intnetR3IfCallSvc(pThis, VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE, &Req.Hdr);
|
---|
425 | }
|
---|
426 |
|
---|
427 |
|
---|
428 | DECLHIDDEN(int) IntNetR3IfSend(INTNETIFCTX hIfCtx)
|
---|
429 | {
|
---|
430 | PINTNETIFCTXINT pThis = hIfCtx;
|
---|
431 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
432 |
|
---|
433 | INTNETIFSENDREQ Req;
|
---|
434 | Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
435 | Req.Hdr.cbReq = sizeof(Req);
|
---|
436 | Req.pSession = pThis->pSupDrvSession;
|
---|
437 | Req.hIf = pThis->hIf;
|
---|
438 | return intnetR3IfCallSvc(pThis, VMMR0_DO_INTNET_IF_SEND, &Req.Hdr);
|
---|
439 | }
|
---|
440 |
|
---|
441 |
|
---|
442 | DECLHIDDEN(int) IntNetR3IfWait(INTNETIFCTX hIfCtx, uint32_t cMillies)
|
---|
443 | {
|
---|
444 | PINTNETIFCTXINT pThis = hIfCtx;
|
---|
445 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
446 |
|
---|
447 | int rc = VINF_SUCCESS;
|
---|
448 | INTNETIFWAITREQ WaitReq;
|
---|
449 | WaitReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
450 | WaitReq.Hdr.cbReq = sizeof(WaitReq);
|
---|
451 | WaitReq.pSession = pThis->pSupDrvSession;
|
---|
452 | WaitReq.hIf = pThis->hIf;
|
---|
453 | WaitReq.cMillies = cMillies;
|
---|
454 | #if defined(VBOX_WITH_INTNET_SERVICE_IN_R3)
|
---|
455 | if (pThis->fIntNetR3Svc)
|
---|
456 | {
|
---|
457 | /* Send an asynchronous message. */
|
---|
458 | rc = intnetR3IfCallSvcAsync(pThis, VMMR0_DO_INTNET_IF_WAIT, &WaitReq.Hdr);
|
---|
459 | if (RT_SUCCESS(rc))
|
---|
460 | {
|
---|
461 | /* Wait on the receive semaphore. */
|
---|
462 | rc = RTSemEventWait(pThis->hEvtRecv, cMillies);
|
---|
463 | }
|
---|
464 | }
|
---|
465 | else
|
---|
466 | #endif
|
---|
467 | rc = intnetR3IfCallSvc(pThis, VMMR0_DO_INTNET_IF_WAIT, &WaitReq.Hdr);
|
---|
468 |
|
---|
469 | return rc;
|
---|
470 | }
|
---|
471 |
|
---|
472 |
|
---|
473 | DECLHIDDEN(int) IntNetR3IfWaitAbort(INTNETIFCTX hIfCtx)
|
---|
474 | {
|
---|
475 | PINTNETIFCTXINT pThis = hIfCtx;
|
---|
476 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
477 |
|
---|
478 | INTNETIFABORTWAITREQ AbortWaitReq;
|
---|
479 | AbortWaitReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
480 | AbortWaitReq.Hdr.cbReq = sizeof(AbortWaitReq);
|
---|
481 | AbortWaitReq.pSession = pThis->pSupDrvSession;
|
---|
482 | AbortWaitReq.hIf = pThis->hIf;
|
---|
483 | AbortWaitReq.fNoMoreWaits = true;
|
---|
484 | return intnetR3IfCallSvc(pThis, VMMR0_DO_INTNET_IF_ABORT_WAIT, &AbortWaitReq.Hdr);
|
---|
485 | }
|
---|
486 |
|
---|
487 |
|
---|
488 | DECLHIDDEN(int) IntNetR3IfPumpPkts(INTNETIFCTX hIfCtx, PFNINPUT pfnInput, void *pvUser,
|
---|
489 | PFNINPUTGSO pfnInputGso, void *pvUserGso)
|
---|
490 | {
|
---|
491 | PINTNETIFCTXINT pThis = hIfCtx;
|
---|
492 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
493 | AssertPtrReturn(pfnInput, VERR_INVALID_POINTER);
|
---|
494 |
|
---|
495 | int rc;
|
---|
496 | for (;;)
|
---|
497 | {
|
---|
498 | rc = IntNetR3IfWait(hIfCtx, RT_INDEFINITE_WAIT);
|
---|
499 | if (RT_SUCCESS(rc) || rc == VERR_INTERRUPTED || rc == VERR_TIMEOUT)
|
---|
500 | {
|
---|
501 | PCINTNETHDR pHdr = IntNetRingGetNextFrameToRead(&pThis->pBuf->Recv);
|
---|
502 | while (pHdr)
|
---|
503 | {
|
---|
504 | const uint8_t u8Type = pHdr->u8Type;
|
---|
505 | void *pvSegFrame;
|
---|
506 | uint32_t cbSegFrame;
|
---|
507 |
|
---|
508 | if (u8Type == INTNETHDR_TYPE_FRAME)
|
---|
509 | {
|
---|
510 | pvSegFrame = IntNetHdrGetFramePtr(pHdr, pThis->pBuf);
|
---|
511 | cbSegFrame = pHdr->cbFrame;
|
---|
512 |
|
---|
513 | /* pass the frame to the user callback */
|
---|
514 | pfnInput(pvUser, pvSegFrame, cbSegFrame);
|
---|
515 | }
|
---|
516 | else if (u8Type == INTNETHDR_TYPE_GSO)
|
---|
517 | {
|
---|
518 | size_t cbGso = pHdr->cbFrame;
|
---|
519 | size_t cbFrame = cbGso - sizeof(PDMNETWORKGSO);
|
---|
520 |
|
---|
521 | PCPDMNETWORKGSO pcGso = IntNetHdrGetGsoContext(pHdr, pThis->pBuf);
|
---|
522 | if (PDMNetGsoIsValid(pcGso, cbGso, cbFrame))
|
---|
523 | {
|
---|
524 | if (pfnInputGso != NULL)
|
---|
525 | {
|
---|
526 | /* pass the frame to the user GSO input callback if set */
|
---|
527 | pfnInputGso(pvUserGso, pcGso, (uint32_t)cbFrame);
|
---|
528 | }
|
---|
529 | else
|
---|
530 | {
|
---|
531 | const uint32_t cSegs = PDMNetGsoCalcSegmentCount(pcGso, cbFrame);
|
---|
532 | for (uint32_t i = 0; i < cSegs; ++i)
|
---|
533 | {
|
---|
534 | uint8_t abHdrScratch[256];
|
---|
535 | pvSegFrame = PDMNetGsoCarveSegmentQD(pcGso, (uint8_t *)(pcGso + 1), cbFrame,
|
---|
536 | abHdrScratch,
|
---|
537 | i, cSegs,
|
---|
538 | &cbSegFrame);
|
---|
539 |
|
---|
540 | /* pass carved frames to the user input callback */
|
---|
541 | pfnInput(pvUser, pvSegFrame, (uint32_t)cbSegFrame);
|
---|
542 | }
|
---|
543 | }
|
---|
544 | }
|
---|
545 | }
|
---|
546 |
|
---|
547 | /* advance to the next input frame */
|
---|
548 | IntNetRingSkipFrame(&pThis->pBuf->Recv);
|
---|
549 | pHdr = IntNetRingGetNextFrameToRead(&pThis->pBuf->Recv);
|
---|
550 | }
|
---|
551 | }
|
---|
552 | else
|
---|
553 | break;
|
---|
554 | }
|
---|
555 | return rc;
|
---|
556 | }
|
---|
557 |
|
---|
558 |
|
---|
559 | DECLHIDDEN(int) IntNetR3IfQueryOutputFrame(INTNETIFCTX hIfCtx, uint32_t cbFrame, PINTNETFRAME pFrame)
|
---|
560 | {
|
---|
561 | PINTNETIFCTXINT pThis = hIfCtx;
|
---|
562 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
563 |
|
---|
564 | return IntNetRingAllocateFrame(&pThis->pBuf->Send, cbFrame, &pFrame->pHdr, &pFrame->pvFrame);
|
---|
565 | }
|
---|
566 |
|
---|
567 |
|
---|
568 | DECLHIDDEN(int) IntNetR3IfOutputFrameCommit(INTNETIFCTX hIfCtx, PCINTNETFRAME pFrame)
|
---|
569 | {
|
---|
570 | PINTNETIFCTXINT pThis = hIfCtx;
|
---|
571 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
572 |
|
---|
573 | IntNetRingCommitFrame(&pThis->pBuf->Send, pFrame->pHdr);
|
---|
574 | return IntNetR3IfSend(hIfCtx);
|
---|
575 | }
|
---|