xref: /qemu/hw/vfio-user/proxy.h (revision 667866d66620e9f545eb3bd4f065d9ab81bda727)
1 #ifndef VFIO_USER_PROXY_H
2 #define VFIO_USER_PROXY_H
3 
4 /*
5  * vfio protocol over a UNIX socket.
6  *
7  * Copyright © 2018, 2021 Oracle and/or its affiliates.
8  *
9  * SPDX-License-Identifier: GPL-2.0-or-later
10  */
11 
12 #include "io/channel.h"
13 #include "io/channel-socket.h"
14 
15 #include "qemu/queue.h"
16 #include "qemu/sockets.h"
17 #include "qemu/thread.h"
18 #include "hw/vfio/vfio-device.h"
19 #include "hw/vfio-user/protocol.h"
20 
21 typedef struct {
22     int send_fds;
23     int recv_fds;
24     int *fds;
25 } VFIOUserFDs;
26 
27 enum msg_type {
28     VFIO_MSG_NONE,
29     VFIO_MSG_ASYNC,
30     VFIO_MSG_WAIT,
31     VFIO_MSG_NOWAIT,
32     VFIO_MSG_REQ,
33 };
34 
35 typedef struct VFIOUserMsg {
36     QTAILQ_ENTRY(VFIOUserMsg) next;
37     VFIOUserHdr *hdr;
38     VFIOUserFDs *fds;
39     uint32_t rsize;
40     uint32_t id;
41     QemuCond cv;
42     bool complete;
43     bool pending;
44     enum msg_type type;
45 } VFIOUserMsg;
46 
47 
48 enum proxy_state {
49     VFIO_PROXY_CONNECTED = 1,
50     VFIO_PROXY_ERROR = 2,
51     VFIO_PROXY_CLOSING = 3,
52     VFIO_PROXY_CLOSED = 4,
53 };
54 
55 typedef QTAILQ_HEAD(VFIOUserMsgQ, VFIOUserMsg) VFIOUserMsgQ;
56 
57 typedef struct VFIOUserProxy {
58     QLIST_ENTRY(VFIOUserProxy) next;
59     char *sockname;
60     struct QIOChannel *ioc;
61     void (*request)(void *opaque, VFIOUserMsg *msg);
62     void *req_arg;
63     uint64_t max_xfer_size;
64     uint64_t max_send_fds;
65     uint64_t max_dma;
66     uint64_t dma_pgsizes;
67     uint64_t max_bitmap;
68     uint64_t migr_pgsize;
69     int flags;
70     QemuCond close_cv;
71     AioContext *ctx;
72     QEMUBH *req_bh;
73 
74     /*
75      * above only changed when BQL is held
76      * below are protected by per-proxy lock
77      */
78     QemuMutex lock;
79     VFIOUserMsgQ free;
80     VFIOUserMsgQ pending;
81     VFIOUserMsgQ incoming;
82     VFIOUserMsgQ outgoing;
83     VFIOUserMsg *last_nowait;
84     VFIOUserMsg *part_recv;
85     size_t recv_left;
86     enum proxy_state state;
87 } VFIOUserProxy;
88 
89 /* VFIOProxy flags */
90 #define VFIO_PROXY_CLIENT        0x1
91 #define VFIO_PROXY_FORCE_QUEUED  0x4
92 
93 typedef struct VFIODevice VFIODevice;
94 
95 VFIOUserProxy *vfio_user_connect_dev(SocketAddress *addr, Error **errp);
96 void vfio_user_disconnect(VFIOUserProxy *proxy);
97 void vfio_user_set_handler(VFIODevice *vbasedev,
98                            void (*handler)(void *opaque, VFIOUserMsg *msg),
99                            void *reqarg);
100 bool vfio_user_validate_version(VFIOUserProxy *proxy, Error **errp);
101 
102 void vfio_user_request_msg(VFIOUserHdr *hdr, uint16_t cmd,
103                            uint32_t size, uint32_t flags);
104 bool vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
105                          VFIOUserFDs *fds, int rsize, Error **errp);
106 
107 #endif /* VFIO_USER_PROXY_H */
108