aboutsummaryrefslogtreecommitdiff
path: root/lib/private.h
blob: 97b24696db97a631b2984b8fca27ca63bc82f3a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Copyright (c) 2019 Nutanix Inc. All rights reserved.
 *
 * Authors: Thanos Makatos <thanos@nutanix.com>
 *          Swapnil Ingle <swapnil.ingle@nutanix.com>
 *          Felipe Franciosi <felipe@nutanix.com>
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *      * Redistributions of source code must retain the above copyright
 *        notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright
 *        notice, this list of conditions and the following disclaimer in the
 *        documentation and/or other materials provided with the distribution.
 *      * Neither the name of Nutanix nor the names of its contributors may be
 *        used to endorse or promote products derived from this software without
 *        specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 *  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 *  DAMAGE.
 *
 */

#ifndef LIB_VFIO_USER_PRIVATE_H
#define LIB_VFIO_USER_PRIVATE_H

#include <errno.h>

#include "pci_caps.h"
#include "common.h"

#define SERVER_MAX_MSG_SIZE 65536

/*
 * Structure used to hold an in-flight request+reply.
 *
 * Incoming request body and fds are stored in in_*.
 *
 * Outgoing requests are either stored in out_data, or out_iovecs. In the latter
 * case, the iovecs refer to data that should not be freed.
 */
typedef struct {
    /* in/out */
    struct vfio_user_header hdr;

    bool processed_cmd;

    int *in_fds;
    size_t nr_in_fds;

    void *in_data;
    size_t in_size;

    int *out_fds;
    size_t nr_out_fds;

    void *out_data;
    size_t out_size;

    struct iovec *out_iovecs;
    size_t nr_out_iovecs;
} vfu_msg_t;

struct transport_ops {
    int (*init)(vfu_ctx_t *vfu_ctx);

    int (*get_poll_fd)(vfu_ctx_t *vfu_ctx);

    int (*attach)(vfu_ctx_t *vfu_ctx);

    int (*get_request_header)(vfu_ctx_t *vfu_ctx, struct vfio_user_header *hdr,
                              int *fds, size_t *nr_fds);

    int (*recv_body)(vfu_ctx_t *vfu_ctx, vfu_msg_t *msg);

    int (*reply)(vfu_ctx_t *vfu_ctx, vfu_msg_t *msg, int err);

    int (*send_msg)(vfu_ctx_t *vfu_ctx, uint16_t msg_id,
                    enum vfio_user_command cmd,
                    void *send_data, size_t send_len,
                    struct vfio_user_header *hdr,
                    void *recv_data, size_t recv_len);

    void (*detach)(vfu_ctx_t *vfu_ctx);
    void (*fini)(vfu_ctx_t *vfu_ctx);
};

typedef struct {
    int         err_efd;    /* eventfd for irq err */
    int         req_efd;    /* eventfd for irq req */
    uint32_t    max_ivs;    /* maximum number of ivs supported */
    int         efds[0];    /* must be last */
} vfu_irqs_t;

struct migration;

typedef struct  {
    /* Region flags, see VFU_REGION_FLAG_READ and friends. */
    uint32_t            flags;
    /* Size of the region. */
    uint32_t            size;
    /* Callback that is called when the region is read or written. */
    vfu_region_access_cb_t  *cb;
    /* Sparse mmap areas if set. */
    struct iovec *mmap_areas;
    int nr_mmap_areas;
    /* fd for a mappable region, or -1. */
    int fd;
} vfu_reg_info_t;

struct pci_dev {
    vfu_pci_type_t          type;
    vfu_pci_config_space_t  *config_space;
    struct pci_cap          caps[VFU_MAX_CAPS];
    size_t                  nr_caps;
    struct pci_cap          ext_caps[VFU_MAX_CAPS];
    size_t                  nr_ext_caps;
};

struct dma_controller;

struct vfu_ctx {
    void                    *pvt;
    struct dma_controller   *dma;
    vfu_reset_cb_t          *reset;
    int           log_level;
    vfu_log_fn_t            *log;
    size_t                  nr_regions;
    vfu_reg_info_t          *reg_info;
    struct pci_dev          pci;
    struct transport_ops    *tran;
    void                    *tran_data;
    uint64_t                flags;
    char                    *uuid;
    vfu_dma_register_cb_t   *dma_register;
    vfu_dma_unregister_cb_t *dma_unregister;

    int                     client_max_fds;

    struct migration        *migration;

    uint32_t                irq_count[VFU_DEV_NUM_IRQS];
    vfu_irqs_t              *irqs;
    bool                    realized;
    vfu_dev_type_t          dev_type;
};

static inline int
ERROR_INT(int err)
{
    errno = err;
    return -1;
}

static inline void *
ERROR_PTR(int err)
{
    errno = err;
    return NULL;
}

void
dump_buffer(const char *prefix, const char *buf, uint32_t count);

int
consume_fd(int *fds, size_t nr_fds, size_t index);

int
handle_dma_map_or_unmap(vfu_ctx_t *vfu_ctx, vfu_msg_t *msg);

int
handle_device_get_region_info(vfu_ctx_t *vfu_ctx, vfu_msg_t *msg);

MOCK_DECLARE(int, handle_dirty_pages, vfu_ctx_t *vfu_ctx, vfu_msg_t *msg);

MOCK_DECLARE(int, get_request_header, vfu_ctx_t *vfu_ctx, vfu_msg_t **msgp);

MOCK_DECLARE(bool, cmd_allowed_when_stopped_and_copying, uint16_t cmd);

MOCK_DECLARE(bool, should_exec_command, vfu_ctx_t *vfu_ctx, uint16_t cmd);

MOCK_DECLARE(int, exec_command, vfu_ctx_t *vfu_ctx, vfu_msg_t *msg);

MOCK_DECLARE(int, process_request, vfu_ctx_t *vfu_ctx);

#endif /* LIB_VFIO_USER_PRIVATE_H */

/* ex: set tabstop=4 shiftwidth=4 softtabstop=4 expandtab: */