Host Guest Communication
In Windows® Virtual PC (WVPC), the virtual machine (guest) and the Windows 7 host need to communicate with each other for sharing data, devices and functionality, such as clipboard sharing, printer sharing, file system and drive sharing, Start menu integration and file association with virtual apps. Such communication is also necessary for displaying the video of the VM on the Windows 7 desktop. In this article, we will discuss in detail how such a guest host communication channel is designed and implemented in WVPC and how you can leverage it to add additional functionality. The communication mechanism is modeled as typical client server architecture with the Windows 7 host acting as the client and the VM acting as the server.
Applications can make use of this feature to start a communication channel and transfer data between the host and guest. The communication channel enables a named pipe client application on the host to exchange data with a TCP server application running in the guest. It is generic and can be used by third-party applications, using its COM APIs which are scriptable. Here are some example applications you could build:
- An application running in the virtual machine could monitor various parameters such as anti-virus status, low disk space, etc. and it can then communicate the status to an application running on the host using its own protocol developed over the communication channel.
- Consider a file created in a virtual application running in a VM, which needs to be printed on a printer connected only to the host. A printer application can be developed to redirect the print command issued in the VM to the host and complete the printing, using the guest-host communication channel.
- An application could be developed to execute a command line tool or script entered on the host command line interface to be redirected to the guest and executed there.
WVPC itself leverages RDP technology from Microsoft Remote Desktop Services® (RDS; formerly Terminal Services) on top of this guest-host communication channel, to show the guest video on the host. When Integration Features are enabled, or a virtual application is started, a TS session is started from host to guest. Traditionally, TS sessions use the network as a communication channel between a client and server. In contrast, since the VM and the host are running on the same hardware, the TS session initiated by WVPC does not require any network connection, but is established using the guest – host communication channel.
Host Guest Communication Channel Architecture
Overall the architecture of host-guest communication channel is shown in Figure 1.
Figure 1. Architectural overview of Host-Guest Communication Channel
In the above diagram, green boxes are application specific, and blue boxes illustrate the host-guest communication protocol implemented in WVPC. To initiate communication, a TS session is established by VMWindow.exe, a component of WVPC, which acts as a named pipe client, with TS server running inside the VM acts as the TCP server. Typically, an application on the host makes a COM call (StartCommunicationChannel) to setup the channel. And then, applications can communicate with the TCP server that is already set up in the guest. StartCommunication COM call starts a named pipe server on the guest and application’s named pipe client can send data to and receive data from this named pipe server. This server is hosted by WVPC Host Process (vpc.exe). On the guest side, a TCP client is established which communicates to the TCP server which is assumed to be up and running when the COM call is made. The TCP client is hosted by Virtual Machine Guest Service (vmsrvc.exe). Under the hood, the named pipe server on the host (vpc.exe) and TCP client on the guest (vmsrvc.exe) communicate with each other using VPCBus. VPCBus is a new integral component of WVPC which provides the communication infrastructure between the WVPC (Virtual PC host process) and the guest VM. The VPCBus achieves this internally by introducing new synthetic devices inside guest VM. These synthetic devices act as communication channels.
As explained in a previous blog, Integration Components need to be installed in the guest to make use of the communication mechanism. Usage of the new communication mechanism is shown in Figure 2:
Figure 2. Usage of the WVPC Guest Host Communication Channel
The entire communication between guest and host happens over VPCBus. For every communication channel established, two VPCBus channels are opened between host and guest. When integration components are enabled, you can see these VPCBus channels in guest device management. These devices come under “Other devices” category and are named “Virtual PC Integration Device”. These are the devices used by WVPC for performing a TS session inside the guest (Fig. 3).
Figure 3. Guest Host Communication Channel components shown in the Device Manager
Named pipe client in the Host
The typical sequence of actions for an application using the communication channel is as follows:
- TCP Server starts in the guest and waits for connection over predefined port number and on LocalHost IP(127.0.0.1). The server can be started manually or be configured to run automatically when guest starts.
- The Client application starts and gets interface handle for the VM in which TCP server is running.
- The client VM opens a named event and waits for it to be signaled. The event name is in predefined format "Local\<ChassisTag>_ChannelReady. The chasis Tag for a VM can be found using IVM interface property ChassisAssetTag.
- When the event gets signaled the client requests the WVPC (Virtual PC host process) to start Named pipe server and establish connection with TCP server in guest. This is done using “StartCommunicationChannel” API. As part of this API client passes the name of Named pipe to be used and the port number on which TCP server is listening.
- WVPC establishes the VPCBus channels with guest. This results in Guest VPCBus component initiating connection with the TCP Server.
- TCP Server accepts connection and waits for Data from client.
- The client then connects to the named pipe using standard Win32 File APIs and starts exchanging data using Read/Write APIs.
With this sequence, host and guest can communicate with each other for the given purpose. For the illustration, let’s take a look at sample named pipe client on the host side which sends “Hello World” to message to guest.
The application begins by getting the interface handle to the virtual machine (line 36) on which the communication is to be done. The VM needs to be running before the StartCommuincationChannel COM call is made. Also, after VM starts execution, it takes time for communication mechanism to be up as it has to wait for the Virtual PC Integration Components Service (service name: 1-vmsrvc) to be started in the guest. Applications can determine when the channel is ready by calling WaitForChannelReady() function (described in detail later).
StartCommunicationChannel takes four arguments – two are end-point types (VMEndpointType), the name with which the named pipe server will be started in the Virtual PC Host Process (\\.\pipe\mynamedpipe in the sample) on the host and the port number on which the TCP server is running ins the guest.
StartCommunicationChannel call (line 63) results in the requested named pipe server starting up in the Virtual PC Host Process (vpc.exe). This can be viewed using Process Explorer as in the screenshot below (Figure 4).
Figure 4. Named pipe server starting up in the Virtual PC Host Process (vpc.exe)
The application then continues to connect (line 75) to the named pipe server it requested and writes out the byte stream (line 90) to be sent to the guest. Behind the scenes, the Virtual PC Host Process will transfer this data over to the Virtual PC Guest Service running in the guest. The guest service acts as a TCP client connecting to the TCP server in the guest and sends the data to it. You can see using TCPView the Virtual PC guest service (vmsrvc.exe) connected to the sample application TCP Server on port 6174 as shown in the screenshot below (Figure 5).
Figure 5. VPC guest service (vmsrvc.exe) connected to the sample TCP Server on port 6174
Host application then disconnects from the named pipe server (line 106). Disconnecting from the named pipe server also causes Virtual PC Host Process to stop this server instance.
Waiting for the Communication Mechanism
Virtual PC Host Process fires a well-known named event to indicate that the communication channel for a virtual machine is ready to be used. The event name is of the format “<vm_chassis_tag>_ChannelReady”. Applications can wait for this event in order to be certain that communication mechanism is up. Below is one way of implementing the wait routine. If StartCommunicationChannel COM call is made before this event is set the call fails with ERROR_NOT_READY.
Sample TCP Server in Guest
The sample TCP Server below prints the data it receives from the host application on the console. The code for the server below is fairly typical and adapted from http://msdn.microsoft.com/en-us/library/ms737593(VS.85).aspx
StartCommunicationChannel and Event Logging
To help debug situations around host-guest communication mechanism, WVPC logs events in the event log. Here is an entry for a failure case:
Figure 6. Example Event Log for a Guest Host Communication Failure Case
Guest-Host communication in WVPC is built as a fast, efficient and extensible client-server style communication mechanism. Further, WVPC has leveraged RDP technology on top of this channel, to enable efficient graphics rendering, drive sharing, seamless virtual applications and other guest-host integration features such as clipboard sharing. Its proximity to the client server architecture and usage of named pipe and TCP interfaces will help enable several communication scenarios between host and guest. With the help of the sample applications presented above, we hope that you can build your own applications making use of the Host-Guest communication mechanism provided in Windows Virtual PC. Check out WVPC and Windows XP Mode today, and let us know what you think, either via the comments section here, or sharing your feedback on the WVPC and Windows XP Mode Forum on Technet here.
Yadnyesh Joshi and Gaurav Sinha
Software Development Engineers
Microsoft Virtualization Team