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