xref: /qemu/hw/vfio-user/proxy.h (revision 36227628d824f563fda95f9344176ca7263c7eaf)
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/sockets.h"
16 #include "hw/vfio-user/protocol.h"
17 
18 typedef struct {
19     int send_fds;
20     int recv_fds;
21     int *fds;
22 } VFIOUserFDs;
23 
24 enum msg_type {
25     VFIO_MSG_NONE,
26     VFIO_MSG_ASYNC,
27     VFIO_MSG_WAIT,
28     VFIO_MSG_NOWAIT,
29     VFIO_MSG_REQ,
30 };
31 
32 typedef struct VFIOUserMsg {
33     QTAILQ_ENTRY(VFIOUserMsg) next;
34     VFIOUserHdr *hdr;
35     VFIOUserFDs *fds;
36     uint32_t rsize;
37     uint32_t id;
38     QemuCond cv;
39     bool complete;
40     bool pending;
41     enum msg_type type;
42 } VFIOUserMsg;
43 
44 
45 enum proxy_state {
46     VFIO_PROXY_CONNECTED = 1,
47     VFIO_PROXY_ERROR = 2,
48     VFIO_PROXY_CLOSING = 3,
49     VFIO_PROXY_CLOSED = 4,
50 };
51 
52 typedef QTAILQ_HEAD(VFIOUserMsgQ, VFIOUserMsg) VFIOUserMsgQ;
53 
54 typedef struct VFIOUserProxy {
55     QLIST_ENTRY(VFIOUserProxy) next;
56     char *sockname;
57     struct QIOChannel *ioc;
58     void (*request)(void *opaque, VFIOUserMsg *msg);
59     void *req_arg;
60     uint64_t max_xfer_size;
61     uint64_t max_send_fds;
62     uint64_t max_dma;
63     uint64_t dma_pgsizes;
64     uint64_t max_bitmap;
65     uint64_t migr_pgsize;
66     int flags;
67     QemuCond close_cv;
68     AioContext *ctx;
69     QEMUBH *req_bh;
70 
71     /*
72      * above only changed when BQL is held
73      * below are protected by per-proxy lock
74      */
75     QemuMutex lock;
76     VFIOUserMsgQ free;
77     VFIOUserMsgQ pending;
78     VFIOUserMsgQ incoming;
79     VFIOUserMsgQ outgoing;
80     VFIOUserMsg *last_nowait;
81     VFIOUserMsg *part_recv;
82     size_t recv_left;
83     enum proxy_state state;
84 } VFIOUserProxy;
85 
86 /* VFIOProxy flags */
87 #define VFIO_PROXY_CLIENT        0x1
88 #define VFIO_PROXY_FORCE_QUEUED  0x4
89 
90 typedef struct VFIODevice VFIODevice;
91 
92 VFIOUserProxy *vfio_user_connect_dev(SocketAddress *addr, Error **errp);
93 void vfio_user_disconnect(VFIOUserProxy *proxy);
94 void vfio_user_set_handler(VFIODevice *vbasedev,
95                            void (*handler)(void *opaque, VFIOUserMsg *msg),
96                            void *reqarg);
97 bool vfio_user_validate_version(VFIOUserProxy *proxy, Error **errp);
98 
99 #endif /* VFIO_USER_PROXY_H */
100