1
/*********************************************************
2
* Copyright (C) 2007 VMware, Inc. All rights reserved.
4
* This program is free software; you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License as published
6
* by the Free Software Foundation version 2.1 and no later version.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10
* or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public
11
* License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program; if not, write to the Free Software Foundation, Inc.,
15
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17
*********************************************************/
22
* VMCI sockets public constants and types.
25
#ifndef _VMCI_SOCKETS_H_
26
#define _VMCI_SOCKETS_H_
30
# if !defined(_DDK_DRIVER_)
31
# include <winsock2.h>
32
# endif // _DDK_DRIVER_
34
#if defined(linux) && !defined(VMKERNEL)
35
# if !defined(__KERNEL__)
36
# include <sys/socket.h>
38
#else // linux && !VMKERNEL
39
# if defined(__APPLE__)
40
# include <sys/socket.h>
43
#endif // linux && !VMKERNEL
47
* We use the same value for the AF family and the socket option
48
* level. To set options, use the value of VMCISock_GetAFValue for
49
* 'level' and these constants for the optname.
51
#define SO_VMCI_BUFFER_SIZE 0
52
#define SO_VMCI_BUFFER_MIN_SIZE 1
53
#define SO_VMCI_BUFFER_MAX_SIZE 2
54
#define SO_VMCI_PEER_HOST_VM_ID 3
57
* The VMCI sockets address equivalents of INADDR_ANY. The first works for
58
* the svm_cid (context id) field of the address structure below and indicates
59
* the current guest (or the host, if running outside a guest), while the
60
* second indicates any available port.
62
#define VMADDR_CID_ANY ((unsigned int) -1)
63
#define VMADDR_PORT_ANY ((unsigned int) -1)
66
#if defined(_WIN32) || defined(VMKERNEL)
67
typedef unsigned short sa_family_t;
72
sa_family_t sa_family;
78
* Address structure for VSockets VMCI sockets. The address family should be
79
* set to AF_VMCI. The structure members should all align on their natural
80
* boundaries without resorting to compiler packing directives.
84
#if defined(__APPLE__)
85
unsigned char svm_len; // Mac OS has the length first.
87
sa_family_t svm_family; // AF_VMCI.
88
unsigned short svm_reserved1; // Reserved.
89
unsigned int svm_port; // Port.
90
unsigned int svm_cid; // Context id.
91
unsigned char svm_zero[sizeof(struct sockaddr) - // Same size as sockaddr.
92
#if defined(__APPLE__)
93
sizeof(unsigned char) -
96
sizeof(unsigned short) -
97
sizeof(unsigned int) -
98
sizeof(unsigned int)];
103
# if !defined(_DDK_DRIVER_)
104
# include <winioctl.h>
105
# define VMCI_SOCKETS_DEVICE L"\\\\.\\VMCI"
106
# define VMCI_SOCKETS_GET_AF_VALUE 0x81032068
107
# define VMCI_SOCKETS_GET_LOCAL_CID 0x8103206c
108
static __inline int VMCISock_GetAFValue(void)
111
HANDLE device = CreateFileW(VMCI_SOCKETS_DEVICE, GENERIC_READ, 0, NULL,
112
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
113
if (INVALID_HANDLE_VALUE != device) {
115
DeviceIoControl(device, VMCI_SOCKETS_GET_AF_VALUE, &afvalue,
116
sizeof afvalue, &afvalue, sizeof afvalue,
119
device = INVALID_HANDLE_VALUE;
124
static __inline unsigned int VMCISock_GetLocalCID(void)
126
unsigned int cid = VMADDR_CID_ANY;
127
HANDLE device = CreateFileW(VMCI_SOCKETS_DEVICE, GENERIC_READ, 0, NULL,
128
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
129
if (INVALID_HANDLE_VALUE != device) {
131
DeviceIoControl(device, VMCI_SOCKETS_GET_LOCAL_CID, &cid,
132
sizeof cid, &cid, sizeof cid, &ioReturn,
135
device = INVALID_HANDLE_VALUE;
139
# endif // _DDK_DRIVER_
141
#if defined(linux) && !defined(VMKERNEL)
143
void VMCISock_KernelRegister(void);
144
void VMCISock_KernelDeregister(void);
145
int VMCISock_GetAFValue(void);
146
int VMCISock_GetLocalCID(void);
148
# include <sys/types.h>
149
# include <sys/stat.h>
151
# include <sys/ioctl.h>
156
# define VMCI_SOCKETS_DEFAULT_DEVICE "/dev/vsock"
157
# define VMCI_SOCKETS_CLASSIC_ESX_DEVICE "/vmfs/devices/char/vsock/vsock"
158
# define IOCTL_VMCI_SOCKETS_GET_AF_VALUE 1976
159
# define IOCTL_VMCI_SOCKETS_GET_LOCAL_CID 1977
162
*----------------------------------------------------------------------------
164
* VMCISock_GetAFValue and VMCISock_GetAFValueFd --
166
* Returns the value to be used for the VMCI Sockets address family.
167
* This value should be used as the domain argument to socket(2) (when
168
* you might otherwise use AF_INET). For VMCI Socket-specific options,
169
* this value should also be used for the level argument to
170
* setsockopt(2) (when you might otherwise use SOL_TCP).
172
* This function leaves its descriptor to the vsock device open so that
173
* the socket implementation knows that the socket family is still in
174
* use. We do this because we register our address family with the
175
* kernel on-demand and need a notification to unregister the address
178
* For many programs this behavior is sufficient as is, but some may
179
* wish to close this descriptor once they are done with VMCI Sockets.
180
* For these programs, we provide a VMCISock_GetAFValueFd() that takes
181
* an optional outFd argument. This value can be provided to
182
* VMCISock_ReleaseAFValueFd() only after the program no longer will
183
* use VMCI Sockets. Note that outFd is only valid in cases where
184
* VMCISock_GetAFValueFd() returns a non-negative value.
187
* The address family value to use on success, negative error code on
190
*----------------------------------------------------------------------------
193
static inline int VMCISock_GetAFValueFd(int *outFd)
198
fd = open(VMCI_SOCKETS_DEFAULT_DEVICE, O_RDWR);
200
fd = open(VMCI_SOCKETS_CLASSIC_ESX_DEVICE, O_RDWR);
206
if (ioctl(fd, IOCTL_VMCI_SOCKETS_GET_AF_VALUE, &family) < 0) {
219
static inline int VMCISock_GetAFValue(void)
221
return VMCISock_GetAFValueFd(NULL);
225
static inline void VMCISock_ReleaseAFValueFd(int fd)
232
static inline unsigned int VMCISock_GetLocalCID(void)
235
unsigned int contextId;
237
fd = open(VMCI_SOCKETS_DEFAULT_DEVICE, O_RDWR);
239
fd = open(VMCI_SOCKETS_CLASSIC_ESX_DEVICE, O_RDWR);
241
return VMADDR_CID_ANY;
245
if (ioctl(fd, IOCTL_VMCI_SOCKETS_GET_LOCAL_CID, &contextId) < 0) {
246
contextId = VMADDR_CID_ANY;
252
# endif // __KERNEL__
254
#if defined(__APPLE__) && !defined(KERNEL)
255
extern int VMCISock_GetAFValue(void);
256
extern unsigned int VMCISock_GetLocalCID(void);
257
#endif // __APPLE__ && !KERNEL
258
#endif // linux && !VMKERNEL
262
#endif // _VMCI_SOCKETS_H_