1 /*
2  * VMware vSockets Driver
3  *
4  * Copyright (C) 2013 VMware, Inc. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation version 2 and no later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  */
15 
16 #ifndef _VMCI_TRANSPORT_H_
17 #define _VMCI_TRANSPORT_H_
18 
19 #include <linux/vmw_vmci_defs.h>
20 #include <linux/vmw_vmci_api.h>
21 
22 #include <net/vsock_addr.h>
23 #include <net/af_vsock.h>
24 
25 /* If the packet format changes in a release then this should change too. */
26 #define VMCI_TRANSPORT_PACKET_VERSION 1
27 
28 /* The resource ID on which control packets are sent. */
29 #define VMCI_TRANSPORT_PACKET_RID 1
30 
31 /* The resource ID on which control packets are sent to the hypervisor. */
32 #define VMCI_TRANSPORT_HYPERVISOR_PACKET_RID 15
33 
34 #define VSOCK_PROTO_INVALID        0
35 #define VSOCK_PROTO_PKT_ON_NOTIFY (1 << 0)
36 #define VSOCK_PROTO_ALL_SUPPORTED (VSOCK_PROTO_PKT_ON_NOTIFY)
37 
38 #define vmci_trans(_vsk) ((struct vmci_transport *)((_vsk)->trans))
39 
40 enum vmci_transport_packet_type {
41 	VMCI_TRANSPORT_PACKET_TYPE_INVALID = 0,
42 	VMCI_TRANSPORT_PACKET_TYPE_REQUEST,
43 	VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE,
44 	VMCI_TRANSPORT_PACKET_TYPE_OFFER,
45 	VMCI_TRANSPORT_PACKET_TYPE_ATTACH,
46 	VMCI_TRANSPORT_PACKET_TYPE_WROTE,
47 	VMCI_TRANSPORT_PACKET_TYPE_READ,
48 	VMCI_TRANSPORT_PACKET_TYPE_RST,
49 	VMCI_TRANSPORT_PACKET_TYPE_SHUTDOWN,
50 	VMCI_TRANSPORT_PACKET_TYPE_WAITING_WRITE,
51 	VMCI_TRANSPORT_PACKET_TYPE_WAITING_READ,
52 	VMCI_TRANSPORT_PACKET_TYPE_REQUEST2,
53 	VMCI_TRANSPORT_PACKET_TYPE_NEGOTIATE2,
54 	VMCI_TRANSPORT_PACKET_TYPE_MAX
55 };
56 
57 struct vmci_transport_waiting_info {
58 	u64 generation;
59 	u64 offset;
60 };
61 
62 /* Control packet type for STREAM sockets.  DGRAMs have no control packets nor
63  * special packet header for data packets, they are just raw VMCI DGRAM
64  * messages.  For STREAMs, control packets are sent over the control channel
65  * while data is written and read directly from queue pairs with no packet
66  * format.
67  */
68 struct vmci_transport_packet {
69 	struct vmci_datagram dg;
70 	u8 version;
71 	u8 type;
72 	u16 proto;
73 	u32 src_port;
74 	u32 dst_port;
75 	u32 _reserved2;
76 	union {
77 		u64 size;
78 		u64 mode;
79 		struct vmci_handle handle;
80 		struct vmci_transport_waiting_info wait;
81 	} u;
82 };
83 
84 struct vmci_transport_notify_pkt {
85 	u64 write_notify_window;
86 	u64 write_notify_min_window;
87 	bool peer_waiting_read;
88 	bool peer_waiting_write;
89 	bool peer_waiting_write_detected;
90 	bool sent_waiting_read;
91 	bool sent_waiting_write;
92 	struct vmci_transport_waiting_info peer_waiting_read_info;
93 	struct vmci_transport_waiting_info peer_waiting_write_info;
94 	u64 produce_q_generation;
95 	u64 consume_q_generation;
96 };
97 
98 struct vmci_transport_notify_pkt_q_state {
99 	u64 write_notify_window;
100 	u64 write_notify_min_window;
101 	bool peer_waiting_write;
102 	bool peer_waiting_write_detected;
103 };
104 
105 union vmci_transport_notify {
106 	struct vmci_transport_notify_pkt pkt;
107 	struct vmci_transport_notify_pkt_q_state pkt_q_state;
108 };
109 
110 /* Our transport-specific data. */
111 struct vmci_transport {
112 	/* For DGRAMs. */
113 	struct vmci_handle dg_handle;
114 	/* For STREAMs. */
115 	struct vmci_handle qp_handle;
116 	struct vmci_qp *qpair;
117 	u64 produce_size;
118 	u64 consume_size;
119 	u64 queue_pair_size;
120 	u64 queue_pair_min_size;
121 	u64 queue_pair_max_size;
122 	u32 detach_sub_id;
123 	union vmci_transport_notify notify;
124 	const struct vmci_transport_notify_ops *notify_ops;
125 	struct list_head elem;
126 	struct sock *sk;
127 	spinlock_t lock; /* protects sk. */
128 };
129 
130 int vmci_transport_register(void);
131 void vmci_transport_unregister(void);
132 
133 int vmci_transport_send_wrote_bh(struct sockaddr_vm *dst,
134 				 struct sockaddr_vm *src);
135 int vmci_transport_send_read_bh(struct sockaddr_vm *dst,
136 				struct sockaddr_vm *src);
137 int vmci_transport_send_wrote(struct sock *sk);
138 int vmci_transport_send_read(struct sock *sk);
139 int vmci_transport_send_waiting_write(struct sock *sk,
140 				      struct vmci_transport_waiting_info *wait);
141 int vmci_transport_send_waiting_read(struct sock *sk,
142 				     struct vmci_transport_waiting_info *wait);
143 
144 #endif
145