xref: /linux/include/uapi/linux/vm_sockets.h (revision ab1c247094e323177a578b38f0325bf79f0317ac)
1e2be04c7SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2d021c344SAndy King /*
3d021c344SAndy King  * VMware vSockets Driver
4d021c344SAndy King  *
5d021c344SAndy King  * Copyright (C) 2007-2013 VMware, Inc. All rights reserved.
6d021c344SAndy King  *
7d021c344SAndy King  * This program is free software; you can redistribute it and/or modify it
8d021c344SAndy King  * under the terms of the GNU General Public License as published by the Free
9d021c344SAndy King  * Software Foundation version 2 and no later version.
10d021c344SAndy King  *
11d021c344SAndy King  * This program is distributed in the hope that it will be useful, but WITHOUT
12d021c344SAndy King  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13d021c344SAndy King  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14d021c344SAndy King  * more details.
15d021c344SAndy King  */
16d021c344SAndy King 
17c031e234SAndy King #ifndef _UAPI_VM_SOCKETS_H
18c031e234SAndy King #define _UAPI_VM_SOCKETS_H
19d021c344SAndy King 
20c031e234SAndy King #include <linux/socket.h>
21dc8eeef7SAndra Paraschiv #include <linux/types.h>
22d021c344SAndy King 
23d021c344SAndy King /* Option name for STREAM socket buffer size.  Use as the option name in
24d021c344SAndy King  * setsockopt(3) or getsockopt(3) to set or get an unsigned long long that
25d021c344SAndy King  * specifies the size of the buffer underlying a vSockets STREAM socket.
26d021c344SAndy King  * Value is clamped to the MIN and MAX.
27d021c344SAndy King  */
28d021c344SAndy King 
29d021c344SAndy King #define SO_VM_SOCKETS_BUFFER_SIZE 0
30d021c344SAndy King 
31d021c344SAndy King /* Option name for STREAM socket minimum buffer size.  Use as the option name
32d021c344SAndy King  * in setsockopt(3) or getsockopt(3) to set or get an unsigned long long that
33d021c344SAndy King  * specifies the minimum size allowed for the buffer underlying a vSockets
34d021c344SAndy King  * STREAM socket.
35d021c344SAndy King  */
36d021c344SAndy King 
37d021c344SAndy King #define SO_VM_SOCKETS_BUFFER_MIN_SIZE 1
38d021c344SAndy King 
39d021c344SAndy King /* Option name for STREAM socket maximum buffer size.  Use as the option name
40d021c344SAndy King  * in setsockopt(3) or getsockopt(3) to set or get an unsigned long long
41d021c344SAndy King  * that specifies the maximum size allowed for the buffer underlying a
42d021c344SAndy King  * vSockets STREAM socket.
43d021c344SAndy King  */
44d021c344SAndy King 
45d021c344SAndy King #define SO_VM_SOCKETS_BUFFER_MAX_SIZE 2
46d021c344SAndy King 
47d021c344SAndy King /* Option name for socket peer's host-specific VM ID.  Use as the option name
48d021c344SAndy King  * in getsockopt(3) to get a host-specific identifier for the peer endpoint's
49d021c344SAndy King  * VM.  The identifier is a signed integer.
50d021c344SAndy King  * Only available for hypervisor endpoints.
51d021c344SAndy King  */
52d021c344SAndy King 
53d021c344SAndy King #define SO_VM_SOCKETS_PEER_HOST_VM_ID 3
54d021c344SAndy King 
55d021c344SAndy King /* Option name for determining if a socket is trusted.  Use as the option name
56d021c344SAndy King  * in getsockopt(3) to determine if a socket is trusted.  The value is a
57d021c344SAndy King  * signed integer.
58d021c344SAndy King  */
59d021c344SAndy King 
60d021c344SAndy King #define SO_VM_SOCKETS_TRUSTED 5
61d021c344SAndy King 
62d021c344SAndy King /* Option name for STREAM socket connection timeout.  Use as the option name
63d021c344SAndy King  * in setsockopt(3) or getsockopt(3) to set or get the connection
64d021c344SAndy King  * timeout for a STREAM socket.
65d021c344SAndy King  */
66d021c344SAndy King 
674c1e34c0SRichard Palethorpe #define SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD 6
68d021c344SAndy King 
69d021c344SAndy King /* Option name for using non-blocking send/receive.  Use as the option name
70d021c344SAndy King  * for setsockopt(3) or getsockopt(3) to set or get the non-blocking
71d021c344SAndy King  * transmit/receive flag for a STREAM socket.  This flag determines whether
72d021c344SAndy King  * send() and recv() can be called in non-blocking contexts for the given
73d021c344SAndy King  * socket.  The value is a signed integer.
74d021c344SAndy King  *
75d021c344SAndy King  * This option is only relevant to kernel endpoints, where descheduling the
76d021c344SAndy King  * thread of execution is not allowed, for example, while holding a spinlock.
77d021c344SAndy King  * It is not to be confused with conventional non-blocking socket operations.
78d021c344SAndy King  *
79d021c344SAndy King  * Only available for hypervisor endpoints.
80d021c344SAndy King  */
81d021c344SAndy King 
82d021c344SAndy King #define SO_VM_SOCKETS_NONBLOCK_TXRX 7
83d021c344SAndy King 
844c1e34c0SRichard Palethorpe #define SO_VM_SOCKETS_CONNECT_TIMEOUT_NEW 8
854c1e34c0SRichard Palethorpe 
864c1e34c0SRichard Palethorpe #if !defined(__KERNEL__)
874c1e34c0SRichard Palethorpe #if __BITS_PER_LONG == 64 || (defined(__x86_64__) && defined(__ILP32__))
884c1e34c0SRichard Palethorpe #define SO_VM_SOCKETS_CONNECT_TIMEOUT SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD
894c1e34c0SRichard Palethorpe #else
904c1e34c0SRichard Palethorpe #define SO_VM_SOCKETS_CONNECT_TIMEOUT \
914c1e34c0SRichard Palethorpe 	(sizeof(time_t) == sizeof(__kernel_long_t) ? SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD : SO_VM_SOCKETS_CONNECT_TIMEOUT_NEW)
924c1e34c0SRichard Palethorpe #endif
934c1e34c0SRichard Palethorpe #endif
944c1e34c0SRichard Palethorpe 
95d021c344SAndy King /* The vSocket equivalent of INADDR_ANY.  This works for the svm_cid field of
96d021c344SAndy King  * sockaddr_vm and indicates the context ID of the current endpoint.
97d021c344SAndy King  */
98d021c344SAndy King 
99d021c344SAndy King #define VMADDR_CID_ANY -1U
100d021c344SAndy King 
101d021c344SAndy King /* Bind to any available port.  Works for the svm_port field of
102d021c344SAndy King  * sockaddr_vm.
103d021c344SAndy King  */
104d021c344SAndy King 
105d021c344SAndy King #define VMADDR_PORT_ANY -1U
106d021c344SAndy King 
107d021c344SAndy King /* Use this as the destination CID in an address when referring to the
108d021c344SAndy King  * hypervisor.  VMCI relies on it being 0, but this would be useful for other
109d021c344SAndy King  * transports too.
110d021c344SAndy King  */
111d021c344SAndy King 
112d021c344SAndy King #define VMADDR_CID_HYPERVISOR 0
113d021c344SAndy King 
114ef343b35SStefano Garzarella /* Use this as the destination CID in an address when referring to the
115ef343b35SStefano Garzarella  * local communication (loopback).
116ef343b35SStefano Garzarella  * (This was VMADDR_CID_RESERVED, but even VMCI doesn't use it anymore,
117ef343b35SStefano Garzarella  * it was a legacy value from an older release).
118d021c344SAndy King  */
119d021c344SAndy King 
120ef343b35SStefano Garzarella #define VMADDR_CID_LOCAL 1
121d021c344SAndy King 
122d021c344SAndy King /* Use this as the destination CID in an address when referring to the host
123d021c344SAndy King  * (any process other than the hypervisor).  VMCI relies on it being 2, but
124d021c344SAndy King  * this would be useful for other transports too.
125d021c344SAndy King  */
126d021c344SAndy King 
127d021c344SAndy King #define VMADDR_CID_HOST 2
128d021c344SAndy King 
129caaf95e0SAndra Paraschiv /* The current default use case for the vsock channel is the following:
130caaf95e0SAndra Paraschiv  * local vsock communication between guest and host and nested VMs setup.
131caaf95e0SAndra Paraschiv  * In addition to this, implicitly, the vsock packets are forwarded to the host
132caaf95e0SAndra Paraschiv  * if no host->guest vsock transport is set.
133caaf95e0SAndra Paraschiv  *
134caaf95e0SAndra Paraschiv  * Set this flag value in the sockaddr_vm corresponding field if the vsock
135caaf95e0SAndra Paraschiv  * packets need to be always forwarded to the host. Using this behavior,
136caaf95e0SAndra Paraschiv  * vsock communication between sibling VMs can be setup.
137caaf95e0SAndra Paraschiv  *
138caaf95e0SAndra Paraschiv  * This way can explicitly distinguish between vsock channels created for
139caaf95e0SAndra Paraschiv  * different use cases, such as nested VMs (or local communication between
140caaf95e0SAndra Paraschiv  * guest and host) and sibling VMs.
141caaf95e0SAndra Paraschiv  *
142caaf95e0SAndra Paraschiv  * The flag can be set in the connect logic in the user space application flow.
143caaf95e0SAndra Paraschiv  * In the listen logic (from kernel space) the flag is set on the remote peer
144caaf95e0SAndra Paraschiv  * address. This happens for an incoming connection when it is routed from the
145caaf95e0SAndra Paraschiv  * host and comes from the guest (local CID and remote CID > VMADDR_CID_HOST).
146caaf95e0SAndra Paraschiv  */
147caaf95e0SAndra Paraschiv #define VMADDR_FLAG_TO_HOST 0x01
148caaf95e0SAndra Paraschiv 
149d021c344SAndy King /* Invalid vSockets version. */
150d021c344SAndy King 
151d021c344SAndy King #define VM_SOCKETS_INVALID_VERSION -1U
152d021c344SAndy King 
153d021c344SAndy King /* The epoch (first) component of the vSockets version.  A single byte
154d021c344SAndy King  * representing the epoch component of the vSockets version.
155d021c344SAndy King  */
156d021c344SAndy King 
157d021c344SAndy King #define VM_SOCKETS_VERSION_EPOCH(_v) (((_v) & 0xFF000000) >> 24)
158d021c344SAndy King 
159d021c344SAndy King /* The major (second) component of the vSockets version.   A single byte
160d021c344SAndy King  * representing the major component of the vSockets version.  Typically
161d021c344SAndy King  * changes for every major release of a product.
162d021c344SAndy King  */
163d021c344SAndy King 
164d021c344SAndy King #define VM_SOCKETS_VERSION_MAJOR(_v) (((_v) & 0x00FF0000) >> 16)
165d021c344SAndy King 
166d021c344SAndy King /* The minor (third) component of the vSockets version.  Two bytes representing
167d021c344SAndy King  * the minor component of the vSockets version.
168d021c344SAndy King  */
169d021c344SAndy King 
170d021c344SAndy King #define VM_SOCKETS_VERSION_MINOR(_v) (((_v) & 0x0000FFFF))
171d021c344SAndy King 
172d021c344SAndy King /* Address structure for vSockets.   The address family should be set to
173c031e234SAndy King  * AF_VSOCK.  The structure members should all align on their natural
174c031e234SAndy King  * boundaries without resorting to compiler packing directives.  The total size
175c031e234SAndy King  * of this structure should be exactly the same as that of struct sockaddr.
176d021c344SAndy King  */
177d021c344SAndy King 
178d021c344SAndy King struct sockaddr_vm {
179c031e234SAndy King 	__kernel_sa_family_t svm_family;
180d021c344SAndy King 	unsigned short svm_reserved1;
181d021c344SAndy King 	unsigned int svm_port;
182d021c344SAndy King 	unsigned int svm_cid;
183dc8eeef7SAndra Paraschiv 	__u8 svm_flags;
184d021c344SAndy King 	unsigned char svm_zero[sizeof(struct sockaddr) -
185d021c344SAndy King 			       sizeof(sa_family_t) -
186d021c344SAndy King 			       sizeof(unsigned short) -
187dc8eeef7SAndra Paraschiv 			       sizeof(unsigned int) -
188dc8eeef7SAndra Paraschiv 			       sizeof(unsigned int) -
189dc8eeef7SAndra Paraschiv 			       sizeof(__u8)];
190d021c344SAndy King };
191d021c344SAndy King 
192d021c344SAndy King #define IOCTL_VM_SOCKETS_GET_LOCAL_CID		_IO(7, 0xb9)
193d021c344SAndy King 
194*49dbe25aSArseniy Krasnov /* MSG_ZEROCOPY notifications are encoded in the standard error format,
195*49dbe25aSArseniy Krasnov  * sock_extended_err. See Documentation/networking/msg_zerocopy.rst in
196*49dbe25aSArseniy Krasnov  * kernel source tree for more details.
197*49dbe25aSArseniy Krasnov  */
198*49dbe25aSArseniy Krasnov 
199*49dbe25aSArseniy Krasnov /* 'cmsg_level' field value of 'struct cmsghdr' for notification parsing
200*49dbe25aSArseniy Krasnov  * when MSG_ZEROCOPY flag is used on transmissions.
201*49dbe25aSArseniy Krasnov  */
202*49dbe25aSArseniy Krasnov 
203*49dbe25aSArseniy Krasnov #define SOL_VSOCK	287
204*49dbe25aSArseniy Krasnov 
205*49dbe25aSArseniy Krasnov /* 'cmsg_type' field value of 'struct cmsghdr' for notification parsing
206*49dbe25aSArseniy Krasnov  * when MSG_ZEROCOPY flag is used on transmissions.
207*49dbe25aSArseniy Krasnov  */
208*49dbe25aSArseniy Krasnov 
209*49dbe25aSArseniy Krasnov #define VSOCK_RECVERR	1
210*49dbe25aSArseniy Krasnov 
211c031e234SAndy King #endif /* _UAPI_VM_SOCKETS_H */
212