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
|
/*
* virtio-fs glue for FUSE
* Copyright (C) 2018 Red Hat, Inc. and/or its affiliates
*
* Authors:
* Dave Gilbert <dgilbert@redhat.com>
*
* Implements the glue between libfuse and libvhost-user
*
* This program can be distributed under the terms of the GNU LGPLv2.
* See the file COPYING.LIB
*/
#include "fuse_i.h"
#include "standard-headers/linux/fuse.h"
#include "fuse_misc.h"
#include "fuse_opt.h"
#include "fuse_virtio.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include "contrib/libvhost-user/libvhost-user.h"
/*
* We pass the dev element into libvhost-user
* and then use it to get back to the outer
* container for other data.
*/
struct fv_VuDev {
VuDev dev;
struct fuse_session *se;
};
/* From spec */
struct virtio_fs_config {
char tag[36];
uint32_t num_queues;
};
/*
* Callback from libvhost-user if there's a new fd we're supposed to listen
* to, typically a queue kick?
*/
static void fv_set_watch(VuDev *dev, int fd, int condition, vu_watch_cb cb,
void *data)
{
fuse_log(FUSE_LOG_WARNING, "%s: TODO! fd=%d\n", __func__, fd);
}
/*
* Callback from libvhost-user if we're no longer supposed to listen on an fd
*/
static void fv_remove_watch(VuDev *dev, int fd)
{
fuse_log(FUSE_LOG_WARNING, "%s: TODO! fd=%d\n", __func__, fd);
}
/* Callback from libvhost-user to panic */
static void fv_panic(VuDev *dev, const char *err)
{
fuse_log(FUSE_LOG_ERR, "%s: libvhost-user: %s\n", __func__, err);
/* TODO: Allow reconnects?? */
exit(EXIT_FAILURE);
}
static bool fv_queue_order(VuDev *dev, int qidx)
{
return false;
}
static const VuDevIface fv_iface = {
/* TODO: Add other callbacks */
.queue_is_processed_in_order = fv_queue_order,
};
int virtio_loop(struct fuse_session *se)
{
fuse_log(FUSE_LOG_INFO, "%s: Entry\n", __func__);
while (1) {
/* TODO: Add stuffing */
}
fuse_log(FUSE_LOG_INFO, "%s: Exit\n", __func__);
}
int virtio_session_mount(struct fuse_session *se)
{
struct sockaddr_un un;
mode_t old_umask;
if (strlen(se->vu_socket_path) >= sizeof(un.sun_path)) {
fuse_log(FUSE_LOG_ERR, "Socket path too long\n");
return -1;
}
se->fd = -1;
/*
* Create the Unix socket to communicate with qemu
* based on QEMU's vhost-user-bridge
*/
unlink(se->vu_socket_path);
strcpy(un.sun_path, se->vu_socket_path);
size_t addr_len = sizeof(un);
int listen_sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (listen_sock == -1) {
fuse_log(FUSE_LOG_ERR, "vhost socket creation: %m\n");
return -1;
}
un.sun_family = AF_UNIX;
/*
* Unfortunately bind doesn't let you set the mask on the socket,
* so set umask to 077 and restore it later.
*/
old_umask = umask(0077);
if (bind(listen_sock, (struct sockaddr *)&un, addr_len) == -1) {
fuse_log(FUSE_LOG_ERR, "vhost socket bind: %m\n");
umask(old_umask);
return -1;
}
umask(old_umask);
if (listen(listen_sock, 1) == -1) {
fuse_log(FUSE_LOG_ERR, "vhost socket listen: %m\n");
return -1;
}
fuse_log(FUSE_LOG_INFO, "%s: Waiting for vhost-user socket connection...\n",
__func__);
int data_sock = accept(listen_sock, NULL, NULL);
if (data_sock == -1) {
fuse_log(FUSE_LOG_ERR, "vhost socket accept: %m\n");
close(listen_sock);
return -1;
}
close(listen_sock);
fuse_log(FUSE_LOG_INFO, "%s: Received vhost-user socket connection\n",
__func__);
/* TODO: Some cleanup/deallocation! */
se->virtio_dev = calloc(sizeof(struct fv_VuDev), 1);
if (!se->virtio_dev) {
fuse_log(FUSE_LOG_ERR, "%s: virtio_dev calloc failed\n", __func__);
close(data_sock);
return -1;
}
se->vu_socketfd = data_sock;
se->virtio_dev->se = se;
vu_init(&se->virtio_dev->dev, 2, se->vu_socketfd, fv_panic, fv_set_watch,
fv_remove_watch, &fv_iface);
return 0;
}
|