1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_VIRTIO_VSOCK_H
3 #define _LINUX_VIRTIO_VSOCK_H
5 #include <uapi/linux/virtio_vsock.h>
6 #include <linux/socket.h>
8 #include <net/af_vsock.h>
10 #define VIRTIO_VSOCK_SKB_HEADROOM (sizeof(struct virtio_vsock_hdr))
12 struct virtio_vsock_skb_cb {
18 #define VIRTIO_VSOCK_SKB_CB(skb) ((struct virtio_vsock_skb_cb *)((skb)->cb))
20 static inline struct virtio_vsock_hdr *virtio_vsock_hdr(struct sk_buff *skb)
22 return (struct virtio_vsock_hdr *)skb->head;
25 static inline bool virtio_vsock_skb_reply(struct sk_buff *skb)
27 return VIRTIO_VSOCK_SKB_CB(skb)->reply;
30 static inline void virtio_vsock_skb_set_reply(struct sk_buff *skb)
32 VIRTIO_VSOCK_SKB_CB(skb)->reply = true;
35 static inline bool virtio_vsock_skb_tap_delivered(struct sk_buff *skb)
37 return VIRTIO_VSOCK_SKB_CB(skb)->tap_delivered;
40 static inline void virtio_vsock_skb_set_tap_delivered(struct sk_buff *skb)
42 VIRTIO_VSOCK_SKB_CB(skb)->tap_delivered = true;
45 static inline void virtio_vsock_skb_clear_tap_delivered(struct sk_buff *skb)
47 VIRTIO_VSOCK_SKB_CB(skb)->tap_delivered = false;
50 static inline void virtio_vsock_skb_rx_put(struct sk_buff *skb)
54 len = le32_to_cpu(virtio_vsock_hdr(skb)->len);
60 static inline struct sk_buff *virtio_vsock_alloc_skb(unsigned int size, gfp_t mask)
64 if (size < VIRTIO_VSOCK_SKB_HEADROOM)
67 skb = alloc_skb(size, mask);
71 skb_reserve(skb, VIRTIO_VSOCK_SKB_HEADROOM);
76 virtio_vsock_skb_queue_head(struct sk_buff_head *list, struct sk_buff *skb)
78 spin_lock_bh(&list->lock);
79 __skb_queue_head(list, skb);
80 spin_unlock_bh(&list->lock);
84 virtio_vsock_skb_queue_tail(struct sk_buff_head *list, struct sk_buff *skb)
86 spin_lock_bh(&list->lock);
87 __skb_queue_tail(list, skb);
88 spin_unlock_bh(&list->lock);
91 static inline struct sk_buff *virtio_vsock_skb_dequeue(struct sk_buff_head *list)
95 spin_lock_bh(&list->lock);
96 skb = __skb_dequeue(list);
97 spin_unlock_bh(&list->lock);
102 static inline void virtio_vsock_skb_queue_purge(struct sk_buff_head *list)
104 spin_lock_bh(&list->lock);
105 __skb_queue_purge(list);
106 spin_unlock_bh(&list->lock);
109 static inline size_t virtio_vsock_skb_len(struct sk_buff *skb)
111 return (size_t)(skb_end_pointer(skb) - skb->head);
114 #define VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE (1024 * 4)
115 #define VIRTIO_VSOCK_MAX_BUF_SIZE 0xFFFFFFFFUL
116 #define VIRTIO_VSOCK_MAX_PKT_BUF_SIZE (1024 * 64)
119 VSOCK_VQ_RX = 0, /* for host to guest data */
120 VSOCK_VQ_TX = 1, /* for guest to host data */
125 /* Per-socket state (accessed via vsk->trans) */
126 struct virtio_vsock_sock {
127 struct vsock_sock *vsk;
132 /* Protected by tx_lock */
137 /* Protected by rx_lock */
142 struct sk_buff_head rx_queue;
146 struct virtio_vsock_pkt_info {
147 u32 remote_cid, remote_port;
148 struct vsock_sock *vsk;
157 struct virtio_transport {
158 /* This must be the first field */
159 struct vsock_transport transport;
161 /* Takes ownership of the packet */
162 int (*send_pkt)(struct sk_buff *skb);
164 /* Used in MSG_ZEROCOPY mode. Checks, that provided data
165 * (number of buffers) could be transmitted with zerocopy
166 * mode. If this callback is not implemented for the current
167 * transport - this means that this transport doesn't need
168 * extra checks and can perform zerocopy transmission by
171 bool (*can_msgzerocopy)(int bufs_num);
175 virtio_transport_stream_dequeue(struct vsock_sock *vsk,
180 virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
182 size_t len, int flags);
185 virtio_transport_seqpacket_enqueue(struct vsock_sock *vsk,
189 virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
192 s64 virtio_transport_stream_has_data(struct vsock_sock *vsk);
193 s64 virtio_transport_stream_has_space(struct vsock_sock *vsk);
194 u32 virtio_transport_seqpacket_has_data(struct vsock_sock *vsk);
196 int virtio_transport_do_socket_init(struct vsock_sock *vsk,
197 struct vsock_sock *psk);
199 virtio_transport_notify_poll_in(struct vsock_sock *vsk,
201 bool *data_ready_now);
203 virtio_transport_notify_poll_out(struct vsock_sock *vsk,
205 bool *space_available_now);
207 int virtio_transport_notify_recv_init(struct vsock_sock *vsk,
208 size_t target, struct vsock_transport_recv_notify_data *data);
209 int virtio_transport_notify_recv_pre_block(struct vsock_sock *vsk,
210 size_t target, struct vsock_transport_recv_notify_data *data);
211 int virtio_transport_notify_recv_pre_dequeue(struct vsock_sock *vsk,
212 size_t target, struct vsock_transport_recv_notify_data *data);
213 int virtio_transport_notify_recv_post_dequeue(struct vsock_sock *vsk,
214 size_t target, ssize_t copied, bool data_read,
215 struct vsock_transport_recv_notify_data *data);
216 int virtio_transport_notify_send_init(struct vsock_sock *vsk,
217 struct vsock_transport_send_notify_data *data);
218 int virtio_transport_notify_send_pre_block(struct vsock_sock *vsk,
219 struct vsock_transport_send_notify_data *data);
220 int virtio_transport_notify_send_pre_enqueue(struct vsock_sock *vsk,
221 struct vsock_transport_send_notify_data *data);
222 int virtio_transport_notify_send_post_enqueue(struct vsock_sock *vsk,
223 ssize_t written, struct vsock_transport_send_notify_data *data);
224 void virtio_transport_notify_buffer_size(struct vsock_sock *vsk, u64 *val);
226 u64 virtio_transport_stream_rcvhiwat(struct vsock_sock *vsk);
227 bool virtio_transport_stream_is_active(struct vsock_sock *vsk);
228 bool virtio_transport_stream_allow(u32 cid, u32 port);
229 int virtio_transport_dgram_bind(struct vsock_sock *vsk,
230 struct sockaddr_vm *addr);
231 bool virtio_transport_dgram_allow(u32 cid, u32 port);
233 int virtio_transport_connect(struct vsock_sock *vsk);
235 int virtio_transport_shutdown(struct vsock_sock *vsk, int mode);
237 void virtio_transport_release(struct vsock_sock *vsk);
240 virtio_transport_stream_enqueue(struct vsock_sock *vsk,
244 virtio_transport_dgram_enqueue(struct vsock_sock *vsk,
245 struct sockaddr_vm *remote_addr,
249 void virtio_transport_destruct(struct vsock_sock *vsk);
251 void virtio_transport_recv_pkt(struct virtio_transport *t,
252 struct sk_buff *skb);
253 void virtio_transport_inc_tx_pkt(struct virtio_vsock_sock *vvs, struct sk_buff *skb);
254 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 wanted);
255 void virtio_transport_put_credit(struct virtio_vsock_sock *vvs, u32 credit);
256 void virtio_transport_deliver_tap_pkt(struct sk_buff *skb);
257 int virtio_transport_purge_skbs(void *vsk, struct sk_buff_head *list);
258 int virtio_transport_read_skb(struct vsock_sock *vsk, skb_read_actor_t read_actor);
259 #endif /* _LINUX_VIRTIO_VSOCK_H */