xref: /qemu/hw/vfio-user/proxy.h (revision 667866d66620e9f545eb3bd4f065d9ab81bda727)
1438d863fSJohn Levon #ifndef VFIO_USER_PROXY_H
2438d863fSJohn Levon #define VFIO_USER_PROXY_H
3438d863fSJohn Levon 
4438d863fSJohn Levon /*
5438d863fSJohn Levon  * vfio protocol over a UNIX socket.
6438d863fSJohn Levon  *
7438d863fSJohn Levon  * Copyright © 2018, 2021 Oracle and/or its affiliates.
8438d863fSJohn Levon  *
9438d863fSJohn Levon  * SPDX-License-Identifier: GPL-2.0-or-later
10438d863fSJohn Levon  */
11438d863fSJohn Levon 
12438d863fSJohn Levon #include "io/channel.h"
13438d863fSJohn Levon #include "io/channel-socket.h"
14438d863fSJohn Levon 
153bdb738bSJohn Levon #include "qemu/queue.h"
160b3d881aSJohn Levon #include "qemu/sockets.h"
173bdb738bSJohn Levon #include "qemu/thread.h"
18*667866d6SJohn Levon #include "hw/vfio/vfio-device.h"
190b3d881aSJohn Levon #include "hw/vfio-user/protocol.h"
200b3d881aSJohn Levon 
21438d863fSJohn Levon typedef struct {
22438d863fSJohn Levon     int send_fds;
23438d863fSJohn Levon     int recv_fds;
24438d863fSJohn Levon     int *fds;
25438d863fSJohn Levon } VFIOUserFDs;
26438d863fSJohn Levon 
27438d863fSJohn Levon enum msg_type {
28438d863fSJohn Levon     VFIO_MSG_NONE,
29438d863fSJohn Levon     VFIO_MSG_ASYNC,
30438d863fSJohn Levon     VFIO_MSG_WAIT,
31438d863fSJohn Levon     VFIO_MSG_NOWAIT,
32438d863fSJohn Levon     VFIO_MSG_REQ,
33438d863fSJohn Levon };
34438d863fSJohn Levon 
35438d863fSJohn Levon typedef struct VFIOUserMsg {
36438d863fSJohn Levon     QTAILQ_ENTRY(VFIOUserMsg) next;
370b3d881aSJohn Levon     VFIOUserHdr *hdr;
38438d863fSJohn Levon     VFIOUserFDs *fds;
39438d863fSJohn Levon     uint32_t rsize;
40438d863fSJohn Levon     uint32_t id;
41438d863fSJohn Levon     QemuCond cv;
42438d863fSJohn Levon     bool complete;
4336227628SJohn Levon     bool pending;
44438d863fSJohn Levon     enum msg_type type;
45438d863fSJohn Levon } VFIOUserMsg;
46438d863fSJohn Levon 
47438d863fSJohn Levon 
48438d863fSJohn Levon enum proxy_state {
49438d863fSJohn Levon     VFIO_PROXY_CONNECTED = 1,
50438d863fSJohn Levon     VFIO_PROXY_ERROR = 2,
51438d863fSJohn Levon     VFIO_PROXY_CLOSING = 3,
52438d863fSJohn Levon     VFIO_PROXY_CLOSED = 4,
53438d863fSJohn Levon };
54438d863fSJohn Levon 
55438d863fSJohn Levon typedef QTAILQ_HEAD(VFIOUserMsgQ, VFIOUserMsg) VFIOUserMsgQ;
56438d863fSJohn Levon 
57438d863fSJohn Levon typedef struct VFIOUserProxy {
58438d863fSJohn Levon     QLIST_ENTRY(VFIOUserProxy) next;
59438d863fSJohn Levon     char *sockname;
60438d863fSJohn Levon     struct QIOChannel *ioc;
61438d863fSJohn Levon     void (*request)(void *opaque, VFIOUserMsg *msg);
62438d863fSJohn Levon     void *req_arg;
6336227628SJohn Levon     uint64_t max_xfer_size;
6436227628SJohn Levon     uint64_t max_send_fds;
6536227628SJohn Levon     uint64_t max_dma;
6636227628SJohn Levon     uint64_t dma_pgsizes;
6736227628SJohn Levon     uint64_t max_bitmap;
6836227628SJohn Levon     uint64_t migr_pgsize;
69438d863fSJohn Levon     int flags;
70438d863fSJohn Levon     QemuCond close_cv;
71438d863fSJohn Levon     AioContext *ctx;
72438d863fSJohn Levon     QEMUBH *req_bh;
73438d863fSJohn Levon 
74438d863fSJohn Levon     /*
75438d863fSJohn Levon      * above only changed when BQL is held
76438d863fSJohn Levon      * below are protected by per-proxy lock
77438d863fSJohn Levon      */
78438d863fSJohn Levon     QemuMutex lock;
79438d863fSJohn Levon     VFIOUserMsgQ free;
80438d863fSJohn Levon     VFIOUserMsgQ pending;
81438d863fSJohn Levon     VFIOUserMsgQ incoming;
82438d863fSJohn Levon     VFIOUserMsgQ outgoing;
83438d863fSJohn Levon     VFIOUserMsg *last_nowait;
840b3d881aSJohn Levon     VFIOUserMsg *part_recv;
850b3d881aSJohn Levon     size_t recv_left;
86438d863fSJohn Levon     enum proxy_state state;
87438d863fSJohn Levon } VFIOUserProxy;
88438d863fSJohn Levon 
89438d863fSJohn Levon /* VFIOProxy flags */
90438d863fSJohn Levon #define VFIO_PROXY_CLIENT        0x1
9136227628SJohn Levon #define VFIO_PROXY_FORCE_QUEUED  0x4
92438d863fSJohn Levon 
930b3d881aSJohn Levon typedef struct VFIODevice VFIODevice;
940b3d881aSJohn Levon 
95438d863fSJohn Levon VFIOUserProxy *vfio_user_connect_dev(SocketAddress *addr, Error **errp);
96438d863fSJohn Levon void vfio_user_disconnect(VFIOUserProxy *proxy);
970b3d881aSJohn Levon void vfio_user_set_handler(VFIODevice *vbasedev,
980b3d881aSJohn Levon                            void (*handler)(void *opaque, VFIOUserMsg *msg),
990b3d881aSJohn Levon                            void *reqarg);
10036227628SJohn Levon bool vfio_user_validate_version(VFIOUserProxy *proxy, Error **errp);
101438d863fSJohn Levon 
1023bdb738bSJohn Levon void vfio_user_request_msg(VFIOUserHdr *hdr, uint16_t cmd,
1033bdb738bSJohn Levon                            uint32_t size, uint32_t flags);
1043bdb738bSJohn Levon bool vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
1053bdb738bSJohn Levon                          VFIOUserFDs *fds, int rsize, Error **errp);
1063bdb738bSJohn Levon 
107438d863fSJohn Levon #endif /* VFIO_USER_PROXY_H */
108