• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_VIRTIO_VSOCK_H
3 #define _LINUX_VIRTIO_VSOCK_H
4 
5 #include <uapi/linux/virtio_vsock.h>
6 #include <linux/socket.h>
7 #include <net/sock.h>
8 #include <net/af_vsock.h>
9 
10 #define VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE	(1024 * 4)
11 #define VIRTIO_VSOCK_MAX_BUF_SIZE		0xFFFFFFFFUL
12 #define VIRTIO_VSOCK_MAX_PKT_BUF_SIZE		(1024 * 64)
13 
14 enum {
15 	VSOCK_VQ_RX     = 0, /* for host to guest data */
16 	VSOCK_VQ_TX     = 1, /* for guest to host data */
17 	VSOCK_VQ_EVENT  = 2,
18 	VSOCK_VQ_MAX    = 3,
19 };
20 
21 /* Per-socket state (accessed via vsk->trans) */
22 struct virtio_vsock_sock {
23 	struct vsock_sock *vsk;
24 
25 	spinlock_t tx_lock;
26 	spinlock_t rx_lock;
27 
28 	/* Protected by tx_lock */
29 	u32 tx_cnt;
30 	u32 peer_fwd_cnt;
31 	u32 peer_buf_alloc;
32 
33 	/* Protected by rx_lock */
34 	u32 fwd_cnt;
35 	u32 last_fwd_cnt;
36 	u32 rx_bytes;
37 	u32 buf_alloc;
38 	struct list_head rx_queue;
39 };
40 
41 struct virtio_vsock_pkt {
42 	struct virtio_vsock_hdr	hdr;
43 	struct list_head list;
44 	/* socket refcnt not held, only use for cancellation */
45 	struct vsock_sock *vsk;
46 	void *buf;
47 	u32 buf_len;
48 	u32 len;
49 	u32 off;
50 	bool reply;
51 	bool tap_delivered;
52 };
53 
54 struct virtio_vsock_pkt_info {
55 	u32 remote_cid, remote_port;
56 	struct vsock_sock *vsk;
57 	struct msghdr *msg;
58 	u32 pkt_len;
59 	u16 type;
60 	u16 op;
61 	u32 flags;
62 	bool reply;
63 };
64 
65 struct virtio_transport {
66 	/* This must be the first field */
67 	struct vsock_transport transport;
68 
69 	/* Takes ownership of the packet */
70 	int (*send_pkt)(struct virtio_vsock_pkt *pkt);
71 };
72 
73 ssize_t
74 virtio_transport_stream_dequeue(struct vsock_sock *vsk,
75 				struct msghdr *msg,
76 				size_t len,
77 				int type);
78 int
79 virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
80 			       struct msghdr *msg,
81 			       size_t len, int flags);
82 
83 s64 virtio_transport_stream_has_data(struct vsock_sock *vsk);
84 s64 virtio_transport_stream_has_space(struct vsock_sock *vsk);
85 
86 int virtio_transport_do_socket_init(struct vsock_sock *vsk,
87 				 struct vsock_sock *psk);
88 int
89 virtio_transport_notify_poll_in(struct vsock_sock *vsk,
90 				size_t target,
91 				bool *data_ready_now);
92 int
93 virtio_transport_notify_poll_out(struct vsock_sock *vsk,
94 				 size_t target,
95 				 bool *space_available_now);
96 
97 int virtio_transport_notify_recv_init(struct vsock_sock *vsk,
98 	size_t target, struct vsock_transport_recv_notify_data *data);
99 int virtio_transport_notify_recv_pre_block(struct vsock_sock *vsk,
100 	size_t target, struct vsock_transport_recv_notify_data *data);
101 int virtio_transport_notify_recv_pre_dequeue(struct vsock_sock *vsk,
102 	size_t target, struct vsock_transport_recv_notify_data *data);
103 int virtio_transport_notify_recv_post_dequeue(struct vsock_sock *vsk,
104 	size_t target, ssize_t copied, bool data_read,
105 	struct vsock_transport_recv_notify_data *data);
106 int virtio_transport_notify_send_init(struct vsock_sock *vsk,
107 	struct vsock_transport_send_notify_data *data);
108 int virtio_transport_notify_send_pre_block(struct vsock_sock *vsk,
109 	struct vsock_transport_send_notify_data *data);
110 int virtio_transport_notify_send_pre_enqueue(struct vsock_sock *vsk,
111 	struct vsock_transport_send_notify_data *data);
112 int virtio_transport_notify_send_post_enqueue(struct vsock_sock *vsk,
113 	ssize_t written, struct vsock_transport_send_notify_data *data);
114 void virtio_transport_notify_buffer_size(struct vsock_sock *vsk, u64 *val);
115 
116 u64 virtio_transport_stream_rcvhiwat(struct vsock_sock *vsk);
117 bool virtio_transport_stream_is_active(struct vsock_sock *vsk);
118 bool virtio_transport_stream_allow(u32 cid, u32 port);
119 int virtio_transport_dgram_bind(struct vsock_sock *vsk,
120 				struct sockaddr_vm *addr);
121 bool virtio_transport_dgram_allow(u32 cid, u32 port);
122 
123 int virtio_transport_connect(struct vsock_sock *vsk);
124 
125 int virtio_transport_shutdown(struct vsock_sock *vsk, int mode);
126 
127 void virtio_transport_release(struct vsock_sock *vsk);
128 
129 ssize_t
130 virtio_transport_stream_enqueue(struct vsock_sock *vsk,
131 				struct msghdr *msg,
132 				size_t len);
133 int
134 virtio_transport_dgram_enqueue(struct vsock_sock *vsk,
135 			       struct sockaddr_vm *remote_addr,
136 			       struct msghdr *msg,
137 			       size_t len);
138 
139 void virtio_transport_destruct(struct vsock_sock *vsk);
140 
141 void virtio_transport_recv_pkt(struct virtio_transport *t,
142 			       struct virtio_vsock_pkt *pkt);
143 void virtio_transport_free_pkt(struct virtio_vsock_pkt *pkt);
144 void virtio_transport_inc_tx_pkt(struct virtio_vsock_sock *vvs, struct virtio_vsock_pkt *pkt);
145 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 wanted);
146 void virtio_transport_put_credit(struct virtio_vsock_sock *vvs, u32 credit);
147 void virtio_transport_deliver_tap_pkt(struct virtio_vsock_pkt *pkt);
148 
149 #endif /* _LINUX_VIRTIO_VSOCK_H */
150