VirtualBox

source: vbox/trunk/src/VBox/Devices/GIMDev/DrvUDP.cpp@ 58390

最後變更 在這個檔案從58390是 58390,由 vboxsync 提交於 9 年 前

VMM/GIM: Implement Hyper-V debug receive thread optimization. Added a few statistics to GIM.
Fixed a bug in Windows guests' debug DHCP handling.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 8.3 KB
 
1/* $Id: DrvUDP.cpp 58390 2015-10-23 12:35:35Z vboxsync $ */
2/** @file
3 * UDP socket stream driver.
4 */
5
6/*
7 * Copyright (C) 2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_DRV_UDP
23#include <VBox/log.h>
24#include <VBox/vmm/pdmdrv.h>
25
26#include "VBoxDD.h"
27
28#include <iprt/socket.h>
29#include <iprt/udp.h>
30#include <iprt/uuid.h>
31
32
33/*********************************************************************************************************************************
34* Defined Constants And Macros *
35*********************************************************************************************************************************/
36/** Converts a pointer to DRVUDP::IStream to a PDRVUDP. */
37#define PDMISTREAM_2_DRVUDP(pInterface) ( (PDRVUDP)((uintptr_t)pInterface - RT_OFFSETOF(DRVUDP, IStream)) )
38
39
40/*********************************************************************************************************************************
41* Structures and Typedefs *
42*********************************************************************************************************************************/
43/**
44 * UDP driver instance data.
45 *
46 * @implements PDMISTREAM
47 */
48typedef struct DRVUDP
49{
50 /** The stream interface. */
51 PDMISTREAM IStream;
52 /** Pointer to the driver instance. */
53 PPDMDRVINS pDrvIns;
54 /** The server port. */
55 uint16_t uServerPort;
56 /** The server address. */
57 char *pszServerAddress;
58 /** The resolved server address struct. */
59 RTNETADDR ServerAddr;
60 /** The UDP socket. */
61 RTSOCKET hSocket;
62} DRVUDP, *PDRVUDP;
63
64
65/*********************************************************************************************************************************
66* Internal Functions *
67*********************************************************************************************************************************/
68
69
70/** @copydoc PDMISTREAM::pfnRead */
71static DECLCALLBACK(int) drvUDPRead(PPDMISTREAM pInterface, void *pvBuf, size_t *pcbRead)
72{
73 int rc = VINF_SUCCESS;
74 PDRVUDP pThis = PDMISTREAM_2_DRVUDP(pInterface);
75 LogFlowFunc(("pvBuf=%p *pcbRead=%#x (%s:%u)\n", pvBuf, *pcbRead, pThis->pszServerAddress, pThis->uServerPort));
76
77 Assert(pvBuf);
78 Assert(pcbRead);
79 if (pThis->hSocket != NIL_RTSOCKET)
80 {
81 size_t cbReallyRead = 0;
82 rc = RTSocketRead(pThis->hSocket, pvBuf, *pcbRead, &cbReallyRead);
83 if (RT_SUCCESS(rc))
84 *pcbRead = cbReallyRead;
85 }
86 else
87 rc = VERR_NET_NOT_SOCKET;
88
89 LogFlowFunc(("*pcbRead=%zu returns %Rrc\n", *pcbRead, rc));
90 return rc;
91}
92
93
94/** @copydoc PDMISTREAM::pfnWrite */
95static DECLCALLBACK(int) drvUDPWrite(PPDMISTREAM pInterface, const void *pvBuf, size_t *pcbWrite)
96{
97 int rc = VINF_SUCCESS;
98 PDRVUDP pThis = PDMISTREAM_2_DRVUDP(pInterface);
99 LogFlowFunc(("pvBuf=%p *pcbWrite=%#x (%s:%u)\n", pvBuf, *pcbWrite, pThis->pszServerAddress, pThis->uServerPort));
100
101 Assert(pvBuf);
102 Assert(pcbWrite);
103 if (pThis->hSocket != NIL_RTSOCKET)
104 {
105 size_t cbBuf = *pcbWrite;
106 rc = RTSocketWriteToNB(pThis->hSocket, pvBuf, cbBuf, NULL /*pDstAddr*/);
107 if (RT_SUCCESS(rc))
108 *pcbWrite = cbBuf;
109 }
110 else
111 rc = VERR_NET_NOT_SOCKET;
112
113 LogFlowFunc(("*pcbWrite=%zu returns %Rrc\n", *pcbWrite, rc));
114 return rc;
115}
116
117
118/**
119 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
120 */
121static DECLCALLBACK(void *) drvUDPQueryInterface(PPDMIBASE pInterface, const char *pszIID)
122{
123 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
124 PDRVUDP pThis = PDMINS_2_DATA(pDrvIns, PDRVUDP);
125 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
126 PDMIBASE_RETURN_INTERFACE(pszIID, PDMISTREAM, &pThis->IStream);
127 return NULL;
128}
129
130
131/* -=-=-=-=- PDMDRVREG -=-=-=-=- */
132
133/**
134 * Destruct a UDP socket stream driver instance.
135 *
136 * Most VM resources are freed by the VM. This callback is provided so that
137 * any non-VM resources can be freed correctly.
138 *
139 * @param pDrvIns The driver instance data.
140 */
141static DECLCALLBACK(void) drvUDPDestruct(PPDMDRVINS pDrvIns)
142{
143 PDRVUDP pThis = PDMINS_2_DATA(pDrvIns, PDRVUDP);
144 LogFlowFunc(("\n"));
145 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
146
147 if (pThis->hSocket != NIL_RTSOCKET)
148 {
149 RTSocketRetain(pThis->hSocket);
150 RTSocketShutdown(pThis->hSocket, true, true);
151 RTSocketClose(pThis->hSocket);
152 pThis->hSocket = NIL_RTSOCKET;
153 LogRel(("DrvUDP#%u: Closed socket to %s:%u\n", pThis->pDrvIns->iInstance, pThis->pszServerAddress, pThis->uServerPort));
154 }
155
156 MMR3HeapFree(pThis->pszServerAddress);
157 pThis->pszServerAddress = NULL;
158}
159
160
161/**
162 * Construct a UDP socket stream driver instance.
163 *
164 * @copydoc FNPDMDRVCONSTRUCT
165 */
166static DECLCALLBACK(int) drvUDPConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
167{
168 PDRVUDP pThis = PDMINS_2_DATA(pDrvIns, PDRVUDP);
169 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
170
171 /*
172 * Init the static parts.
173 */
174 pThis->pDrvIns = pDrvIns;
175 /* IBase */
176 pDrvIns->IBase.pfnQueryInterface = drvUDPQueryInterface;
177 /* IStream */
178 pThis->IStream.pfnRead = drvUDPRead;
179 pThis->IStream.pfnWrite = drvUDPWrite;
180
181 /*
182 * Validate and read the configuration.
183 */
184 PDMDRV_VALIDATE_CONFIG_RETURN(pDrvIns, "ServerAddress|ServerPort", "");
185
186 int rc = CFGMR3QueryStringAlloc(pCfg, "ServerAddress", &pThis->pszServerAddress);
187 if (RT_FAILURE(rc))
188 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
189 N_("Configuration error: querying \"ServerAddress\" resulted in %Rrc"), rc);
190 rc = CFGMR3QueryU16(pCfg, "ServerPort", &pThis->uServerPort);
191 if (RT_FAILURE(rc))
192 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
193 N_("Configuration error: querying \"ServerPort\" resulted in %Rrc"), rc);
194
195 /*
196 * Create the socket and connect.
197 */
198 rc = RTUdpCreateClientSocket(pThis->pszServerAddress, pThis->uServerPort, NULL, &pThis->hSocket);
199 if (RT_SUCCESS(rc))
200 {
201 LogRel(("DrvUDP#%u: Connected socket to %s:%u\n", pThis->pDrvIns->iInstance, pThis->pszServerAddress,
202 pThis->uServerPort));
203 }
204 else
205 {
206 LogRel(("DrvUDP#%u: Failed to create/connect socket to %s:%u rc=%Rrc\n", pThis->pDrvIns->iInstance,
207 pThis->pszServerAddress, pThis->uServerPort, rc));
208 }
209 return VINF_SUCCESS;
210}
211
212
213/**
214 * UDP socket driver registration record.
215 */
216const PDMDRVREG g_DrvUDP =
217{
218 /* u32Version */
219 PDM_DRVREG_VERSION,
220 /* szName */
221 "UDP",
222 /* szRCMod */
223 "",
224 /* szR0Mod */
225 "",
226 /* pszDescription */
227 "UDP socket stream driver.",
228 /* fFlags */
229 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
230 /* fClass. */
231 PDM_DRVREG_CLASS_STREAM,
232 /* cMaxInstances */
233 ~0U,
234 /* cbInstance */
235 sizeof(DRVUDP),
236 /* pfnConstruct */
237 drvUDPConstruct,
238 /* pfnDestruct */
239 drvUDPDestruct,
240 /* pfnRelocate */
241 NULL,
242 /* pfnIOCtl */
243 NULL,
244 /* pfnPowerOn */
245 NULL,
246 /* pfnReset */
247 NULL,
248 /* pfnSuspend */
249 NULL,
250 /* pfnResume */
251 NULL,
252 /* pfnAttach */
253 NULL,
254 /* pfnDetach */
255 NULL,
256 /* pfnPowerOff */
257 NULL,
258 /* pfnSoftReset */
259 NULL,
260 /* u32EndVersion */
261 PDM_DRVREG_VERSION
262};
263
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette