VMCI Sockets Programming Guide VMware ESXi 5.5 VMware Workstation 9.x This document supports the version of each product listed and supports all subsequent versions until the document is replaced by a new edition. To check for more recent editions of this document, see http://www.vmware.com/support/pubs.
VMCI Sockets Programming Guide You can find the most up-to-date technical documentation on the VMware Web site at: http://www.vmware.com/support/ The VMware Web site also provides the latest product updates. If you have comments about this documentation, submit your feedback to: docfeedback@vmware.com Copyright © 2008–2013 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws.
Contents About This Book 5 1 About VMCI Sockets 7 Introduction to VMCI Sockets 7 Previous VMCI Releases 7 How VMCI Sockets Work 7 Persistence of Sockets 8 Socket Programming 8 Features in Specific VMware Releases 8 Enabling and Finding VMCI Sockets 8 Location of Include File for C Programs 8 Security of VMCI Sockets 9 Use Cases for VMCI Sockets 9 RabbitMQ with Stream VMCI Sockets 9 Network Storage with Datagram VMCI Sockets 10 2 Porting to VMCI Sockets 11 Porting Existing Socket Applications 11 Include
VMCI Sockets Programming Guide Recv() Function 18 Close() Function 18 Poll() Information 18 Read() and Write() 18 4 Creating Datagram VMCI Sockets 19 Datagram VMCI Sockets 19 Preparing the Server for a Connection 20 Socket() Function 20 Socket Options 20 Bind() Function 20 Getsockname() Function 21 Recvfrom() Function 21 Sendto() Function 21 Close() Function 21 Having the Client Request a Connection 21 Socket() Function 21 Sendto() Function 22 Connect() and Send() 22 Recvfrom() Function 22 Close() Functio
About This Book The VMware® VMCI Sockets Programming Guide describes how to program virtual machine communications interface (VMCI) sockets. The VMCI sockets API facilitates fast and efficient communication between guest virtual machines and their host. Revision History VMware revises this guide with each release of the product or when necessary. A revised version can contain minor or major changes. Table 1 summarizes the significant changes in each version of this guide. Table 1.
VMCI Sockets Programming Guide VMware Technical Publications Glossary VMware Technical Publications provides a glossary of terms that might be unfamiliar to you. For definitions of terms as they are used in VMware technical documentation go to http://www.vmware.com/support/pubs. # VMware, Inc.
1 About VMCI Sockets 1 This chapter includes the following topics: “Introduction to VMCI Sockets” on page 7 “Features in Specific VMware Releases” on page 8 “Enabling and Finding VMCI Sockets” on page 8 “Use Cases for VMCI Sockets” on page 9 This guide assumes that you know about either Berkeley sockets or Winsock, the Windows implementation. If you are new to sockets, see “Appendix: Learning More About Sockets” on page 25.
VMCI Sockets Programming Guide Persistence of Sockets VMCI sockets lose connection after suspend and resume of a virtual machine. In VMware vSphere with ESX/ESXi hosts and vCenter Server, VMCI sockets do not survive live migration with VMware vMotion™ from source to destination host. In VMware vSphere with ESX/ESXi hosts, VMCI stream socket connections are dropped when a virtual machine is put into fault tolerance (FT) mode.
Chapter 1 About VMCI Sockets Security of VMCI Sockets VMCI Sockets are more secure after elimination of guest to guest communications. For an overview of VMCI security, see Chapter 5, “Security of the VMCI Device,” on page 23.
VMCI Sockets Programming Guide RabbitMQ software is an open‐source message broker (or message‐oriented middleware) that implements the AMQP standard. SpringSource currently develops and supports RabbitMQ. Network Storage with Datagram VMCI Sockets Figure 1‐2 shows an example of a VMware host acting as the NFS server for the home directories of its three clients: a Windows guest and two Linux guests. NFS uses datagram sockets for file I/O.
2 Porting to VMCI Sockets 2 This chapter includes the following topics: “Porting Existing Socket Applications” on page 11 “Communicating Between Host and Guest” on page 12 Porting Existing Socket Applications Modifying existing socket implementations is straightforward. This chapter describes the lines of code you must change. Include a New Header File To obtain the definitions for VMCI sockets, include the vmci_sockets.h header file. #include "vmci_sockets.
VMCI Sockets Programming Guide Connection-Oriented Stream Socket To establish a stream socket, include these declarations and calls, and replace AF_INET with afVMCI, as set by VMCISock_GetAFValue().
3 Creating Stream VMCI Sockets 3 This chapter describes the details of creating VMCI sockets to replace TCP stream sockets. “Preparing the Server for a Connection” on page 14 “Having the Client Request a Connection” on page 17 Stream VMCI Sockets The flowchart in Figure 3‐1 shows how to establish connection‐oriented sockets on the server and client. Figure 3-1.
VMCI Sockets Programming Guide Preparing the Server for a Connection At the top of your application, include vmci_sockets.h and declare a constant for the socket buffer size. In the example below, BUFSIZE defines the socket buffer size. The number 4096 is a good choice for efficiency on multiple platforms. It is not based on the size of a TCP packet, which is usually smaller. #include "vmci_sockets.h" #define BUFSIZE 4096 To compile on Windows, you must also call the Winsock WSAStartup() function.
Chapter 3 Creating Stream VMCI Sockets Bind() Function This bind() call associates the stream socket with the network settings in the sockaddr_vm structure, instead of the sockaddr_in structure. struct sockaddr_vm my_addr = {0}; my_addr.svm_family = afVMCI; my_addr.svm_cid = VMADDR_CID_ANY; my_addr.
VMCI Sockets Programming Guide Recv() Function The recv() call reads data from the client application. The server and client can communicate the length of data transmitted, or the server can terminate its recv() loop when the client closes its connection. char recv_buf[BUFSIZE]; if ((numbytes = recv(sockfd, recv_buf, sizeof recv_buf, 0)) == -1) { perror("recv"); goto close; } Send() Function The send() call writes data to the client application.
Chapter 3 Creating Stream VMCI Sockets Having the Client Request a Connection At the top of your application, include vmci_sockets.h and declare a constant for the socket buffer size. In the example below, BUFSIZE defines the socket buffer size. It is not based on the size of a TCP packet. #include "vmci_sockets.h" #define BUFSIZE 4096 To compile on Windows, you must call the Winsock WSAStartup() function. See “Preparing the Server for a Connection” on page 14 for sample code.
VMCI Sockets Programming Guide Recv() Function The recv() call reads data from the server application. Sometimes the server sends flow control information, so the client must be prepared to receive it. Use the same socket descriptor as for send(). char recv_buf[BUFSIZE]; if ((numbytes = recv(sockfd, recv_buf, sizeof recv_buf, 0)) == -1) { perror("recv"); goto close; } Close() Function The close() call shuts down a connection, given the original socket descriptor obtained from the socket() function.
4 Creating Datagram VMCI Sockets 4 This chapter describes the details of creating VMCI sockets to replace UDP sockets. “Preparing the Server for a Connection” on page 20 “Having the Client Request a Connection” on page 21 Datagram VMCI Sockets The flowchart in Figure 4‐1 shows how to establish connectionless sockets on the server and client. Figure 4-1.
VMCI Sockets Programming Guide Preparing the Server for a Connection At the top of your application, include vmci_sockets.h and declare a constant for the socket buffer size. In the example below, BUFSIZE defines the socket buffer size. The number 4096 is a good choice for efficiency on multiple platforms. It is not based on the size of a UDP datagram. #include "vmci_sockets.h" #define BUFSIZE 4096 To compile on Windows, you must call the Winsock WSAStartup() function.
Chapter 4 Creating Datagram VMCI Sockets Getsockname() Function The getsockname() function retrieves the local address associated with a socket. my_addr_size = sizeof my_addr; if (getsockname(sockfd, (struct sockaddr *) &my_addr, &my_addr_size) == -1) { perror("getsockname"); goto close; } Recvfrom() Function The recvfrom() call reads data from the client application.
VMCI Sockets Programming Guide Sendto() Function Because this is a connectionless protocol, you pass the socket address structure their_addr as a parameter to the sendto() call. struct sockaddr_vm their_addr = {0}; their_addr.svm_family = afVMCI; their_addr.svm_cid = SERVER_CID; their_addr.
5 Security of the VMCI Device 5 This chapter provides background information about security of the VMCI device, especially about interfaces that are not part of the public VMCI Sockets API. VMCI Sockets are implemented on top of the VMCI device. “Interfaces for VMCI Settings” on page 23 “VMCI Device Always Enabled” on page 23 “Isolation of Virtual Machines” on page 24 Interfaces for VMCI Settings VMCI is used primarily for communication between virtual machines and the hypervisor.
VMCI Sockets Programming Guide Isolation Options in VMX ESX/ESXi 4.0 to ESXi 5.0 provide .vmx options for VMCI isolation. As of ESXi 5.1, these options have no effect. [vmci0.unrestricted = FALSE|TRUE] When its vmci.unrestricted option was set TRUE, a virtual machine could communicate with all host endpoints and other virtual machines that had vmci0.unrestricted set TRUE. [vmci0.
Appendix: Learning More About Sockets This appendix introduces Internet sockets and provides pointers to further information. “About Berkeley Sockets and Winsock” on page 25 “Short Introduction to Sockets” on page 26 About Berkeley Sockets and Winsock A socket is a communications endpoint with a name and address in a network. Sockets were made famous by their implementation in Berkeley UNIX, and made universal by their incorporation into Windows.
VMCI Sockets Programming Guide Microsoft Winsock The Winsock Programmer’s FAQ is an excellent introduction to Windows sockets. Currently it is hosted by the http://tangentsoft.net Web site. For complete reference information about Winsock, refer to the public MSDN Web site. Short Introduction to Sockets Network I/O is similar to file I/O, although network I/O requires not only a file descriptor sufficient for identifying a file, but also sufficient information for network communication.
Appendix: Learning More About Sockets In the sockaddr structure for IPv4 sockets, the first field specifies AF_INET. The second field sin_port can be any integer > 5000. Lower port numbers are reserved for specific services. The third field in_addr is the Internet address in dotted‐quad notation. For the server, you can use the constant INADDR_ANY to tell the system to accept a connection on any Internet interface for the system. Conversion functions htons() and htonl() are for hardware independence.
VMCI Sockets Programming Guide This is similar to the accept() system call, except that the client does not have to bind a local address to the socket descriptor before calling connect(). The server address pointed to by srv_addr must exist. For example: #define SERV_PORT 5432 unsigned long inet_addr(char *ptr); bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(SERV_PORT): serv_addr.sin_addr.
Index A P about VMCI sockets 7 accept() 15 address structure for sockets 12 AF_INET and PF_INET 25 AF_INET and VMCI sockets 11 afVMCI from VMCISock_GetAFValue() 12, 14, 17 AMQP (advanced message queuing protocol) 9 Apache and Firefox VMCI sockets 9 PF_INET and AF_INET 25 poll() 16, 18 port number, VMADDR_PORT_ANY 12, 15 porting sockets applications 11 B bind() 12, 15, 20 books about sockets 25 R RabbitMQ 9, 10 read() 16, 18 recv() 16, 18 recvfrom() 21 release contents 8 S C close() 16, 18, 21 connect
VMCI Sockets Programming Guide 30 VMware, Inc.