blob: b139f76060a65ec1b20301bb263b3ee2c6328144 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/* 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_MIN_BUF_SIZE 128
11#define VIRTIO_VSOCK_DEFAULT_BUF_SIZE (1024 * 256)
12#define VIRTIO_VSOCK_DEFAULT_MAX_BUF_SIZE (1024 * 256)
13#define VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE (1024 * 4)
14#define VIRTIO_VSOCK_MAX_BUF_SIZE 0xFFFFFFFFUL
15#define VIRTIO_VSOCK_MAX_PKT_BUF_SIZE (1024 * 64)
16
17enum {
18 VSOCK_VQ_RX = 0, /* for host to guest data */
19 VSOCK_VQ_TX = 1, /* for guest to host data */
20 VSOCK_VQ_EVENT = 2,
21 VSOCK_VQ_MAX = 3,
22};
23
24/* Per-socket state (accessed via vsk->trans) */
25struct virtio_vsock_sock {
26 struct vsock_sock *vsk;
27
28 /* Protected by lock_sock(sk_vsock(trans->vsk)) */
29 u32 buf_size;
30 u32 buf_size_min;
31 u32 buf_size_max;
32
33 spinlock_t tx_lock;
34 spinlock_t rx_lock;
35
36 /* Protected by tx_lock */
37 u32 tx_cnt;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000038 u32 peer_fwd_cnt;
39 u32 peer_buf_alloc;
40
41 /* Protected by rx_lock */
42 u32 fwd_cnt;
David Brazdil0f672f62019-12-10 10:32:29 +000043 u32 last_fwd_cnt;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000044 u32 rx_bytes;
David Brazdil0f672f62019-12-10 10:32:29 +000045 u32 buf_alloc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000046 struct list_head rx_queue;
47};
48
49struct virtio_vsock_pkt {
50 struct virtio_vsock_hdr hdr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000051 struct list_head list;
52 /* socket refcnt not held, only use for cancellation */
53 struct vsock_sock *vsk;
54 void *buf;
David Brazdil0f672f62019-12-10 10:32:29 +000055 u32 buf_len;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000056 u32 len;
57 u32 off;
58 bool reply;
59};
60
61struct virtio_vsock_pkt_info {
62 u32 remote_cid, remote_port;
63 struct vsock_sock *vsk;
64 struct msghdr *msg;
65 u32 pkt_len;
66 u16 type;
67 u16 op;
68 u32 flags;
69 bool reply;
70};
71
72struct virtio_transport {
73 /* This must be the first field */
74 struct vsock_transport transport;
75
76 /* Takes ownership of the packet */
77 int (*send_pkt)(struct virtio_vsock_pkt *pkt);
78};
79
80ssize_t
81virtio_transport_stream_dequeue(struct vsock_sock *vsk,
82 struct msghdr *msg,
83 size_t len,
84 int type);
85int
86virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
87 struct msghdr *msg,
88 size_t len, int flags);
89
90s64 virtio_transport_stream_has_data(struct vsock_sock *vsk);
91s64 virtio_transport_stream_has_space(struct vsock_sock *vsk);
92
93int virtio_transport_do_socket_init(struct vsock_sock *vsk,
94 struct vsock_sock *psk);
95u64 virtio_transport_get_buffer_size(struct vsock_sock *vsk);
96u64 virtio_transport_get_min_buffer_size(struct vsock_sock *vsk);
97u64 virtio_transport_get_max_buffer_size(struct vsock_sock *vsk);
98void virtio_transport_set_buffer_size(struct vsock_sock *vsk, u64 val);
99void virtio_transport_set_min_buffer_size(struct vsock_sock *vsk, u64 val);
100void virtio_transport_set_max_buffer_size(struct vsock_sock *vs, u64 val);
101int
102virtio_transport_notify_poll_in(struct vsock_sock *vsk,
103 size_t target,
104 bool *data_ready_now);
105int
106virtio_transport_notify_poll_out(struct vsock_sock *vsk,
107 size_t target,
108 bool *space_available_now);
109
110int virtio_transport_notify_recv_init(struct vsock_sock *vsk,
111 size_t target, struct vsock_transport_recv_notify_data *data);
112int virtio_transport_notify_recv_pre_block(struct vsock_sock *vsk,
113 size_t target, struct vsock_transport_recv_notify_data *data);
114int virtio_transport_notify_recv_pre_dequeue(struct vsock_sock *vsk,
115 size_t target, struct vsock_transport_recv_notify_data *data);
116int virtio_transport_notify_recv_post_dequeue(struct vsock_sock *vsk,
117 size_t target, ssize_t copied, bool data_read,
118 struct vsock_transport_recv_notify_data *data);
119int virtio_transport_notify_send_init(struct vsock_sock *vsk,
120 struct vsock_transport_send_notify_data *data);
121int virtio_transport_notify_send_pre_block(struct vsock_sock *vsk,
122 struct vsock_transport_send_notify_data *data);
123int virtio_transport_notify_send_pre_enqueue(struct vsock_sock *vsk,
124 struct vsock_transport_send_notify_data *data);
125int virtio_transport_notify_send_post_enqueue(struct vsock_sock *vsk,
126 ssize_t written, struct vsock_transport_send_notify_data *data);
127
128u64 virtio_transport_stream_rcvhiwat(struct vsock_sock *vsk);
129bool virtio_transport_stream_is_active(struct vsock_sock *vsk);
130bool virtio_transport_stream_allow(u32 cid, u32 port);
131int virtio_transport_dgram_bind(struct vsock_sock *vsk,
132 struct sockaddr_vm *addr);
133bool virtio_transport_dgram_allow(u32 cid, u32 port);
134
135int virtio_transport_connect(struct vsock_sock *vsk);
136
137int virtio_transport_shutdown(struct vsock_sock *vsk, int mode);
138
139void virtio_transport_release(struct vsock_sock *vsk);
140
141ssize_t
142virtio_transport_stream_enqueue(struct vsock_sock *vsk,
143 struct msghdr *msg,
144 size_t len);
145int
146virtio_transport_dgram_enqueue(struct vsock_sock *vsk,
147 struct sockaddr_vm *remote_addr,
148 struct msghdr *msg,
149 size_t len);
150
151void virtio_transport_destruct(struct vsock_sock *vsk);
152
Olivier Deprez0e641232021-09-23 10:07:05 +0200153void virtio_transport_recv_pkt(struct virtio_transport *t,
154 struct virtio_vsock_pkt *pkt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000155void virtio_transport_free_pkt(struct virtio_vsock_pkt *pkt);
156void virtio_transport_inc_tx_pkt(struct virtio_vsock_sock *vvs, struct virtio_vsock_pkt *pkt);
157u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 wanted);
158void virtio_transport_put_credit(struct virtio_vsock_sock *vvs, u32 credit);
159void virtio_transport_deliver_tap_pkt(struct virtio_vsock_pkt *pkt);
160
161#endif /* _LINUX_VIRTIO_VSOCK_H */