VirtualBox

source: vbox/trunk/src/VBox/NetworkServices/NetLib/IntNetIfCtx.cpp@ 97074

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

NetworkServices: Implement support for communicating over the R3 internal network service in driverless mode, bugref:10297 [doxygen fix]

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.3 KB
Line 
1/* $Id: IntNetIfCtx.cpp 97074 2022-10-10 16:36:11Z 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 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 */
65typedef 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. */
87typedef 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 */
102static int intnetR3IfCtxCallSvc(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 int rc = (int)xpc_dictionary_get_int64(hObjReply, "rc");
114
115 size_t cbReply = 0;
116 const void *pvData = xpc_dictionary_get_data(hObjReply, "reply", &cbReply);
117 AssertRelease(cbReply == cbReq);
118 memcpy(pReqHdr, pvData, cbReq);
119 xpc_release(hObjReply);
120
121 return rc;
122# endif
123 }
124 else
125#else
126 RT_NOREF(pThis);
127#endif
128 return SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, uOperation, 0, pReqHdr);
129}
130
131
132#if defined(RT_OS_DARWIN) && defined(VBOX_WITH_INTNET_SERVICE_IN_R3)
133/**
134 * Calls the internal networking switch service living in either R0 or in another R3 process.
135 *
136 * @returns VBox status code.
137 * @param pThis The internal network driver instance data.
138 * @param uOperation The operation to execute.
139 * @param pReqHdr Pointer to the request header.
140 */
141static int intnetR3IfCtxCallSvcAsync(PINTNETIFCTXINT pThis, uint32_t uOperation, PSUPVMMR0REQHDR pReqHdr)
142{
143 if (pThis->fIntNetR3Svc)
144 {
145 xpc_object_t hObj = xpc_dictionary_create(NULL, NULL, 0);
146 xpc_dictionary_set_uint64(hObj, "req-id", uOperation);
147 xpc_dictionary_set_data(hObj, "req", pReqHdr, pReqHdr->cbReq);
148 xpc_connection_send_message(pThis->hXpcCon, hObj);
149 return VINF_SUCCESS;
150 }
151 else
152 return SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, uOperation, 0, pReqHdr);
153}
154#endif
155
156
157/**
158 * Map the ring buffer pointer into this process R3 address space.
159 *
160 * @returns VBox status code.
161 * @param pThis The internal network driver instance data.
162 */
163static int intnetR3IfCtxMapBufferPointers(PINTNETIFCTXINT pThis)
164{
165 int rc = VINF_SUCCESS;
166
167 INTNETIFGETBUFFERPTRSREQ GetBufferPtrsReq;
168 GetBufferPtrsReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
169 GetBufferPtrsReq.Hdr.cbReq = sizeof(GetBufferPtrsReq);
170 GetBufferPtrsReq.pSession = pThis->pSupDrvSession;
171 GetBufferPtrsReq.hIf = pThis->hIf;
172 GetBufferPtrsReq.pRing3Buf = NULL;
173 GetBufferPtrsReq.pRing0Buf = NIL_RTR0PTR;
174
175#if defined(VBOX_WITH_INTNET_SERVICE_IN_R3)
176 if (pThis->fIntNetR3Svc)
177 {
178#if defined(RT_OS_DARWIN)
179 xpc_object_t hObj = xpc_dictionary_create(NULL, NULL, 0);
180 xpc_dictionary_set_uint64(hObj, "req-id", VMMR0_DO_INTNET_IF_GET_BUFFER_PTRS);
181 xpc_dictionary_set_data(hObj, "req", &GetBufferPtrsReq, sizeof(GetBufferPtrsReq));
182 xpc_object_t hObjReply = xpc_connection_send_message_with_reply_sync(pThis->hXpcCon, hObj);
183 rc = (int)xpc_dictionary_get_int64(hObjReply, "rc");
184 if (RT_SUCCESS(rc))
185 {
186 /* Get the shared memory object. */
187 xpc_object_t hObjShMem = xpc_dictionary_get_value(hObjReply, "buf-ptr");
188 size_t cbMem = xpc_shmem_map(hObjShMem, (void **)&pThis->pBuf);
189 if (!cbMem)
190 rc = VERR_NO_MEMORY;
191 else
192 pThis->cbBuf = cbMem;
193 }
194 xpc_release(hObjReply);
195#endif
196 }
197 else
198#endif
199 {
200 rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_INTNET_IF_GET_BUFFER_PTRS, 0 /*u64Arg*/, &GetBufferPtrsReq.Hdr);
201 if (RT_SUCCESS(rc))
202 {
203 AssertRelease(RT_VALID_PTR(GetBufferPtrsReq.pRing3Buf));
204 pThis->pBuf = GetBufferPtrsReq.pRing3Buf;
205 }
206 }
207
208 return rc;
209}
210
211
212static void intnetR3IfCtxClose(PINTNETIFCTXINT pThis)
213{
214 if (pThis->hIf != INTNET_HANDLE_INVALID)
215 {
216 INTNETIFCLOSEREQ CloseReq;
217 CloseReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
218 CloseReq.Hdr.cbReq = sizeof(CloseReq);
219 CloseReq.pSession = pThis->pSupDrvSession;
220 CloseReq.hIf = pThis->hIf;
221
222 pThis->hIf = INTNET_HANDLE_INVALID;
223 int rc = intnetR3IfCtxCallSvc(pThis, VMMR0_DO_INTNET_IF_CLOSE, &CloseReq.Hdr);
224 AssertRC(rc);
225 }
226}
227
228
229DECLHIDDEN(int) IntNetR3IfCtxCreate(PINTNETIFCTX phIfCtx, const char *pszNetwork, INTNETTRUNKTYPE enmTrunkType,
230 const char *pszTrunk, size_t cbSend, size_t cbRecv, uint32_t fFlags)
231{
232 AssertPtrReturn(phIfCtx, VERR_INVALID_POINTER);
233 AssertPtrReturn(pszNetwork, VERR_INVALID_POINTER);
234 AssertPtrReturn(pszTrunk, VERR_INVALID_POINTER);
235
236 PSUPDRVSESSION pSession = NIL_RTR0PTR;
237 int rc = SUPR3Init(&pSession);
238 if (RT_SUCCESS(rc))
239 {
240 PINTNETIFCTXINT pThis = (PINTNETIFCTXINT)RTMemAllocZ(sizeof(*pThis));
241 if (RT_LIKELY(pThis))
242 {
243 pThis->pSupDrvSession = pSession;
244#if defined(VBOX_WITH_INTNET_SERVICE_IN_R3)
245 pThis->hEvtRecv = NIL_RTSEMEVENT;
246#endif
247
248 /* Driverless operation needs support for running the internal network switch using IPC. */
249 if (SUPR3IsDriverless())
250 {
251#if defined(VBOX_WITH_INTNET_SERVICE_IN_R3)
252# if defined(RT_OS_DARWIN)
253 xpc_connection_t hXpcCon = xpc_connection_create(INTNET_R3_SVC_NAME, NULL);
254 xpc_connection_set_event_handler(hXpcCon, ^(xpc_object_t hObj) {
255 if (xpc_get_type(hObj) == XPC_TYPE_ERROR)
256 {
257 /** @todo Error handling - reconnecting. */
258 }
259 else
260 {
261 /* Out of band messages should only come when there is something to receive. */
262 RTSemEventSignal(pThis->hEvtRecv);
263 }
264 });
265
266 xpc_connection_activate(hXpcCon);
267 pThis->hXpcCon = hXpcCon;
268# endif
269 pThis->fIntNetR3Svc = true;
270 rc = RTSemEventCreate(&pThis->hEvtRecv);
271#else
272 rc = VERR_SUP_DRIVERLESS;
273#endif
274 }
275 else
276 {
277 /* Need to load VMMR0.r0 containing the network switching code. */
278 char szPathVMMR0[RTPATH_MAX];
279
280 rc = RTPathExecDir(szPathVMMR0, sizeof(szPathVMMR0));
281 if (RT_SUCCESS(rc))
282 {
283 rc = RTPathAppend(szPathVMMR0, sizeof(szPathVMMR0), "VMMR0.r0");
284 if (RT_SUCCESS(rc))
285 rc = SUPR3LoadVMM(szPathVMMR0, /* :pErrInfo */ NULL);
286 }
287 }
288
289 if (RT_SUCCESS(rc))
290 {
291 /* Open the interface. */
292 INTNETOPENREQ OpenReq;
293 RT_ZERO(OpenReq);
294
295 OpenReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
296 OpenReq.Hdr.cbReq = sizeof(OpenReq);
297 OpenReq.pSession = pThis->pSupDrvSession;
298 OpenReq.enmTrunkType = enmTrunkType;
299 OpenReq.fFlags = fFlags;
300 OpenReq.cbSend = cbSend;
301 OpenReq.cbRecv = cbRecv;
302 OpenReq.hIf = INTNET_HANDLE_INVALID;
303
304 rc = RTStrCopy(OpenReq.szNetwork, sizeof(OpenReq.szNetwork), pszNetwork);
305 if (RT_SUCCESS(rc))
306 rc = RTStrCopy(OpenReq.szTrunk, sizeof(OpenReq.szTrunk), pszTrunk);
307 if (RT_SUCCESS(rc))
308 {
309 rc = intnetR3IfCtxCallSvc(pThis, VMMR0_DO_INTNET_OPEN, &OpenReq.Hdr);
310 if (RT_SUCCESS(rc))
311 {
312 pThis->hIf = OpenReq.hIf;
313
314 rc = intnetR3IfCtxMapBufferPointers(pThis);
315 if (RT_SUCCESS(rc))
316 {
317 *phIfCtx = pThis;
318 return VINF_SUCCESS;
319 }
320 }
321
322 intnetR3IfCtxClose(pThis);
323 }
324 }
325
326#if defined(VBOX_WITH_INTNET_SERVICE_IN_R3)
327 if (pThis->fIntNetR3Svc)
328 {
329# if defined(RT_OS_DARWIN)
330 if (pThis->hXpcCon)
331 xpc_connection_cancel(pThis->hXpcCon);
332 pThis->hXpcCon = NULL;
333# endif
334
335 if (pThis->hEvtRecv != NIL_RTSEMEVENT)
336 RTSemEventDestroy(pThis->hEvtRecv);
337 }
338#endif
339
340 RTMemFree(pThis);
341 }
342
343 SUPR3Term();
344 }
345
346 return rc;
347}
348
349
350DECLHIDDEN(int) IntNetR3IfCtxDestroy(INTNETIFCTX hIfCtx)
351{
352 PINTNETIFCTXINT pThis = hIfCtx;
353 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
354
355 intnetR3IfCtxClose(pThis);
356
357#if defined(VBOX_WITH_INTNET_SERVICE_IN_R3)
358 if (pThis->fIntNetR3Svc)
359 {
360# if defined(RT_OS_DARWIN)
361 /* Unmap the shared buffer. */
362 munmap(pThis->pBuf, pThis->cbBuf);
363 xpc_connection_cancel(pThis->hXpcCon);
364 pThis->hXpcCon = NULL;
365# endif
366 RTSemEventDestroy(pThis->hEvtRecv);
367 pThis->fIntNetR3Svc = false;
368 }
369#endif
370
371 RTMemFree(pThis);
372 return VINF_SUCCESS;
373}
374
375
376DECLHIDDEN(int) IntNetR3IfCtxQueryBufferPtr(INTNETIFCTX hIfCtx, PINTNETBUF *ppIfBuf)
377{
378 PINTNETIFCTXINT pThis = hIfCtx;
379 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
380 AssertPtrReturn(ppIfBuf, VERR_INVALID_POINTER);
381
382 *ppIfBuf = pThis->pBuf;
383 return VINF_SUCCESS;
384}
385
386
387DECLHIDDEN(int) IntNetR3IfCtxSetActive(INTNETIFCTX hIfCtx, bool fActive)
388{
389 PINTNETIFCTXINT pThis = hIfCtx;
390 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
391
392 INTNETIFSETACTIVEREQ Req;
393 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
394 Req.Hdr.cbReq = sizeof(Req);
395 Req.pSession = pThis->pSupDrvSession;
396 Req.hIf = pThis->hIf;
397 Req.fActive = fActive;
398 return intnetR3IfCtxCallSvc(pThis, VMMR0_DO_INTNET_IF_SET_ACTIVE, &Req.Hdr);
399}
400
401
402DECLHIDDEN(int) IntNetR3IfCtxSetPromiscuous(INTNETIFCTX hIfCtx, bool fPromiscuous)
403{
404 PINTNETIFCTXINT pThis = hIfCtx;
405 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
406
407 INTNETIFSETPROMISCUOUSMODEREQ Req;
408 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
409 Req.Hdr.cbReq = sizeof(Req);
410 Req.pSession = pThis->pSupDrvSession;
411 Req.hIf = pThis->hIf;
412 Req.fPromiscuous = fPromiscuous;
413 return intnetR3IfCtxCallSvc(pThis, VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE, &Req.Hdr);
414}
415
416
417DECLHIDDEN(int) IntNetR3IfSend(INTNETIFCTX hIfCtx)
418{
419 PINTNETIFCTXINT pThis = hIfCtx;
420 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
421
422 INTNETIFSENDREQ Req;
423 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
424 Req.Hdr.cbReq = sizeof(Req);
425 Req.pSession = pThis->pSupDrvSession;
426 Req.hIf = pThis->hIf;
427 return intnetR3IfCtxCallSvc(pThis, VMMR0_DO_INTNET_IF_SEND, &Req.Hdr);
428}
429
430
431DECLHIDDEN(int) IntNetR3IfWait(INTNETIFCTX hIfCtx, uint32_t cMillies)
432{
433 PINTNETIFCTXINT pThis = hIfCtx;
434 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
435
436 int rc = VINF_SUCCESS;
437 INTNETIFWAITREQ WaitReq;
438 WaitReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
439 WaitReq.Hdr.cbReq = sizeof(WaitReq);
440 WaitReq.pSession = pThis->pSupDrvSession;
441 WaitReq.hIf = pThis->hIf;
442 WaitReq.cMillies = cMillies;
443#if defined(VBOX_WITH_INTNET_SERVICE_IN_R3)
444 if (pThis->fIntNetR3Svc)
445 {
446 /* Send an asynchronous message. */
447 rc = intnetR3IfCtxCallSvcAsync(pThis, VMMR0_DO_INTNET_IF_WAIT, &WaitReq.Hdr);
448 if (RT_SUCCESS(rc))
449 {
450 /* Wait on the receive semaphore. */
451 rc = RTSemEventWait(pThis->hEvtRecv, cMillies);
452 }
453 }
454 else
455#endif
456 rc = intnetR3IfCtxCallSvc(pThis, VMMR0_DO_INTNET_IF_WAIT, &WaitReq.Hdr);
457
458 return rc;
459}
460
461
462DECLHIDDEN(int) IntNetR3IfWaitAbort(INTNETIFCTX hIfCtx)
463{
464 PINTNETIFCTXINT pThis = hIfCtx;
465 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
466
467 INTNETIFABORTWAITREQ AbortWaitReq;
468 AbortWaitReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
469 AbortWaitReq.Hdr.cbReq = sizeof(AbortWaitReq);
470 AbortWaitReq.pSession = pThis->pSupDrvSession;
471 AbortWaitReq.hIf = pThis->hIf;
472 AbortWaitReq.fNoMoreWaits = true;
473 return intnetR3IfCtxCallSvc(pThis, VMMR0_DO_INTNET_IF_ABORT_WAIT, &AbortWaitReq.Hdr);
474}
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