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