Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2016 Intel Corporation
3 : : */
4 : :
5 : : #include <stdint.h>
6 : : #include <stdio.h>
7 : : #include <limits.h>
8 : : #include <stdlib.h>
9 : : #include <unistd.h>
10 : : #include <string.h>
11 : : #include <sys/socket.h>
12 : : #include <sys/un.h>
13 : : #include <sys/queue.h>
14 : : #include <errno.h>
15 : : #include <fcntl.h>
16 : :
17 : : #include <eal_export.h>
18 : : #include <rte_thread.h>
19 : : #include <rte_log.h>
20 : :
21 : : #include "fd_man.h"
22 : : #include "vduse.h"
23 : : #include "vhost.h"
24 : : #include "vhost_user.h"
25 : :
26 : :
27 : : TAILQ_HEAD(vhost_user_connection_list, vhost_user_connection);
28 : :
29 : : /*
30 : : * Every time rte_vhost_driver_register() is invoked, an associated
31 : : * vhost_user_socket struct will be created.
32 : : */
33 : : struct vhost_user_socket {
34 : : struct vhost_user_connection_list conn_list;
35 : : pthread_mutex_t conn_mutex;
36 : : char *path;
37 : : int socket_fd;
38 : : struct sockaddr_un un;
39 : : bool is_server;
40 : : bool is_vduse;
41 : : bool reconnect;
42 : : bool iommu_support;
43 : : bool use_builtin_virtio_net;
44 : : bool extbuf;
45 : : bool linearbuf;
46 : : bool async_copy;
47 : : bool net_compliant_ol_flags;
48 : : bool stats_enabled;
49 : : bool async_connect;
50 : :
51 : : /*
52 : : * The "supported_features" indicates the feature bits the
53 : : * vhost driver supports. The "features" indicates the feature
54 : : * bits after the rte_vhost_driver_features_disable/enable().
55 : : * It is also the final feature bits used for vhost-user
56 : : * features negotiation.
57 : : */
58 : : uint64_t supported_features;
59 : : uint64_t features;
60 : :
61 : : uint64_t protocol_features;
62 : :
63 : : uint32_t max_queue_pairs;
64 : :
65 : : struct rte_vdpa_device *vdpa_dev;
66 : :
67 : : struct rte_vhost_device_ops const *notify_ops;
68 : : };
69 : :
70 : : struct vhost_user_connection {
71 : : struct vhost_user_socket *vsocket;
72 : : int connfd;
73 : : int vid;
74 : :
75 : : TAILQ_ENTRY(vhost_user_connection) next;
76 : : };
77 : :
78 : : #define MAX_VHOST_SOCKET 1024
79 : : struct vhost_user {
80 : : struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
81 : : struct fdset *fdset;
82 : : int vsocket_cnt;
83 : : pthread_mutex_t mutex;
84 : : };
85 : :
86 : : #define MAX_VIRTIO_BACKLOG 128
87 : :
88 : : static void vhost_user_server_new_connection(int fd, void *data, int *close);
89 : : static void vhost_user_read_cb(int fd, void *dat, int *close);
90 : : static int create_unix_socket(struct vhost_user_socket *vsocket);
91 : : static int vhost_user_start_client(struct vhost_user_socket *vsocket);
92 : :
93 : : static struct vhost_user vhost_user = {
94 : : .vsocket_cnt = 0,
95 : : .mutex = PTHREAD_MUTEX_INITIALIZER,
96 : : };
97 : :
98 : : /*
99 : : * return bytes# of read on success or negative val on failure. Update fdnum
100 : : * with number of fds read.
101 : : */
102 : : int
103 : 0 : read_fd_message(char *ifname, int sockfd, char *buf, int buflen, int *fds, int max_fds,
104 : : int *fd_num)
105 : 0 : {
106 : : struct iovec iov;
107 : : struct msghdr msgh;
108 : 0 : char control[CMSG_SPACE(max_fds * sizeof(int))];
109 : : struct cmsghdr *cmsg;
110 : : int got_fds = 0;
111 : : int ret;
112 : :
113 : 0 : *fd_num = 0;
114 : :
115 : : memset(&msgh, 0, sizeof(msgh));
116 : 0 : iov.iov_base = buf;
117 : 0 : iov.iov_len = buflen;
118 : :
119 : 0 : msgh.msg_iov = &iov;
120 : 0 : msgh.msg_iovlen = 1;
121 : 0 : msgh.msg_control = control;
122 : 0 : msgh.msg_controllen = sizeof(control);
123 : :
124 : 0 : ret = recvmsg(sockfd, &msgh, 0);
125 [ # # ]: 0 : if (ret <= 0) {
126 [ # # ]: 0 : if (ret)
127 : 0 : VHOST_CONFIG_LOG(ifname, ERR, "recvmsg failed on fd %d (%s)",
128 : : sockfd, strerror(errno));
129 : 0 : return ret;
130 : : }
131 : :
132 [ # # ]: 0 : if (msgh.msg_flags & MSG_TRUNC)
133 : 0 : VHOST_CONFIG_LOG(ifname, ERR, "truncated msg (fd %d)", sockfd);
134 : :
135 : : /* MSG_CTRUNC may be caused by LSM misconfiguration */
136 [ # # ]: 0 : if (msgh.msg_flags & MSG_CTRUNC)
137 : 0 : VHOST_CONFIG_LOG(ifname, ERR, "truncated control data (fd %d)", sockfd);
138 : :
139 [ # # # # ]: 0 : for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
140 : : cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
141 [ # # ]: 0 : if ((cmsg->cmsg_level == SOL_SOCKET) &&
142 : : (cmsg->cmsg_type == SCM_RIGHTS)) {
143 : 0 : got_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
144 : 0 : *fd_num = got_fds;
145 : 0 : memcpy(fds, CMSG_DATA(cmsg), got_fds * sizeof(int));
146 : : break;
147 : : }
148 : : }
149 : :
150 : : /* Clear out unused file descriptors */
151 [ # # ]: 0 : while (got_fds < max_fds)
152 : 0 : fds[got_fds++] = -1;
153 : :
154 : : return ret;
155 : : }
156 : :
157 : : int
158 : 0 : send_fd_message(char *ifname, int sockfd, char *buf, int buflen, int *fds, int fd_num)
159 : 0 : {
160 : :
161 : : struct iovec iov;
162 : : struct msghdr msgh;
163 : 0 : size_t fdsize = fd_num * sizeof(int);
164 [ # # ]: 0 : char control[CMSG_SPACE(fdsize)];
165 : : struct cmsghdr *cmsg;
166 : : int ret;
167 : :
168 : : memset(&msgh, 0, sizeof(msgh));
169 : 0 : iov.iov_base = buf;
170 : 0 : iov.iov_len = buflen;
171 : :
172 : 0 : msgh.msg_iov = &iov;
173 : 0 : msgh.msg_iovlen = 1;
174 : :
175 [ # # ]: 0 : if (fds && fd_num > 0) {
176 : 0 : msgh.msg_control = control;
177 : 0 : msgh.msg_controllen = sizeof(control);
178 [ # # ]: 0 : cmsg = CMSG_FIRSTHDR(&msgh);
179 : : if (cmsg == NULL) {
180 : 0 : VHOST_CONFIG_LOG(ifname, ERR, "cmsg == NULL");
181 : 0 : errno = EINVAL;
182 : 0 : return -1;
183 : : }
184 : 0 : cmsg->cmsg_len = CMSG_LEN(fdsize);
185 : 0 : cmsg->cmsg_level = SOL_SOCKET;
186 : 0 : cmsg->cmsg_type = SCM_RIGHTS;
187 : 0 : memcpy(CMSG_DATA(cmsg), fds, fdsize);
188 : : } else {
189 : : msgh.msg_control = NULL;
190 : : msgh.msg_controllen = 0;
191 : : }
192 : :
193 : : do {
194 : 0 : ret = sendmsg(sockfd, &msgh, MSG_NOSIGNAL);
195 [ # # # # ]: 0 : } while (ret < 0 && errno == EINTR);
196 : :
197 [ # # ]: 0 : if (ret < 0) {
198 : 0 : VHOST_CONFIG_LOG(ifname, ERR, "sendmsg error on fd %d (%s)",
199 : : sockfd, strerror(errno));
200 : 0 : return ret;
201 : : }
202 : :
203 : : return ret;
204 : : }
205 : :
206 : : static void
207 : 0 : vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
208 : : {
209 : : int vid;
210 : : struct vhost_user_connection *conn;
211 : : int ret;
212 : : struct virtio_net *dev;
213 : :
214 [ # # ]: 0 : if (vsocket == NULL)
215 : : return;
216 : :
217 : 0 : conn = malloc(sizeof(*conn));
218 [ # # ]: 0 : if (conn == NULL) {
219 : 0 : close(fd);
220 : 0 : return;
221 : : }
222 : :
223 : 0 : vid = vhost_user_new_device();
224 [ # # ]: 0 : if (vid == -1) {
225 : 0 : goto err;
226 : : }
227 : :
228 : 0 : vhost_set_ifname(vid, vsocket->path);
229 : :
230 : 0 : vhost_setup_virtio_net(vid, vsocket->use_builtin_virtio_net,
231 : 0 : vsocket->net_compliant_ol_flags, vsocket->stats_enabled,
232 : 0 : vsocket->iommu_support);
233 : :
234 : 0 : vhost_attach_vdpa_device(vid, vsocket->vdpa_dev);
235 : :
236 [ # # ]: 0 : if (vsocket->extbuf)
237 : 0 : vhost_enable_extbuf(vid);
238 : :
239 [ # # ]: 0 : if (vsocket->linearbuf)
240 : 0 : vhost_enable_linearbuf(vid);
241 : :
242 [ # # ]: 0 : if (vsocket->async_copy) {
243 : : dev = get_device(vid);
244 : :
245 [ # # ]: 0 : if (dev)
246 : 0 : dev->async_copy = 1;
247 : : }
248 : :
249 : 0 : VHOST_CONFIG_LOG(vsocket->path, INFO, "new device, handle is %d", vid);
250 : :
251 [ # # ]: 0 : if (vsocket->notify_ops->new_connection) {
252 : 0 : ret = vsocket->notify_ops->new_connection(vid);
253 [ # # ]: 0 : if (ret < 0) {
254 : 0 : VHOST_CONFIG_LOG(vsocket->path, ERR,
255 : : "failed to add vhost user connection with fd %d",
256 : : fd);
257 : 0 : goto err_cleanup;
258 : : }
259 : : }
260 : :
261 : 0 : conn->connfd = fd;
262 : 0 : conn->vsocket = vsocket;
263 : 0 : conn->vid = vid;
264 : 0 : ret = fdset_add(vhost_user.fdset, fd, vhost_user_read_cb,
265 : : NULL, conn);
266 [ # # ]: 0 : if (ret < 0) {
267 : 0 : VHOST_CONFIG_LOG(vsocket->path, ERR,
268 : : "failed to add fd %d into vhost server fdset",
269 : : fd);
270 : :
271 [ # # ]: 0 : if (vsocket->notify_ops->destroy_connection)
272 : 0 : vsocket->notify_ops->destroy_connection(conn->vid);
273 : :
274 : 0 : goto err_cleanup;
275 : : }
276 : :
277 : 0 : pthread_mutex_lock(&vsocket->conn_mutex);
278 : 0 : TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
279 : 0 : pthread_mutex_unlock(&vsocket->conn_mutex);
280 : :
281 : 0 : return;
282 : :
283 : 0 : err_cleanup:
284 : 0 : vhost_destroy_device(vid);
285 : 0 : err:
286 : 0 : free(conn);
287 : 0 : close(fd);
288 : : }
289 : :
290 : : /* call back when there is new vhost-user connection from client */
291 : : static void
292 : 0 : vhost_user_server_new_connection(int fd, void *dat, int *close __rte_unused)
293 : : {
294 : : struct vhost_user_socket *vsocket = dat;
295 : :
296 : 0 : fd = accept(fd, NULL, NULL);
297 [ # # ]: 0 : if (fd < 0)
298 : : return;
299 : :
300 : 0 : VHOST_CONFIG_LOG(vsocket->path, INFO, "new vhost user connection is %d", fd);
301 : 0 : vhost_user_add_connection(fd, vsocket);
302 : : }
303 : :
304 : : static void
305 : 0 : vhost_user_read_cb(int connfd, void *dat, int *close)
306 : : {
307 : : struct vhost_user_connection *conn = dat;
308 : 0 : struct vhost_user_socket *vsocket = conn->vsocket;
309 : : int ret;
310 : :
311 : 0 : ret = vhost_user_msg_handler(conn->vid, connfd);
312 [ # # ]: 0 : if (ret < 0) {
313 [ # # ]: 0 : struct virtio_net *dev = get_device(conn->vid);
314 : :
315 : 0 : *close = 1;
316 : :
317 [ # # ]: 0 : if (dev)
318 : 0 : vhost_destroy_device_notify(dev);
319 : :
320 [ # # ]: 0 : if (vsocket->notify_ops->destroy_connection)
321 : 0 : vsocket->notify_ops->destroy_connection(conn->vid);
322 : :
323 : 0 : vhost_destroy_device(conn->vid);
324 : :
325 [ # # ]: 0 : if (vsocket->reconnect) {
326 : 0 : create_unix_socket(vsocket);
327 : 0 : vhost_user_start_client(vsocket);
328 : : }
329 : :
330 : 0 : pthread_mutex_lock(&vsocket->conn_mutex);
331 [ # # ]: 0 : TAILQ_REMOVE(&vsocket->conn_list, conn, next);
332 : 0 : pthread_mutex_unlock(&vsocket->conn_mutex);
333 : :
334 : 0 : free(conn);
335 : : }
336 : 0 : }
337 : :
338 : : static int
339 : 0 : create_unix_socket(struct vhost_user_socket *vsocket)
340 : : {
341 : : int fd;
342 : 0 : struct sockaddr_un *un = &vsocket->un;
343 : :
344 : 0 : fd = socket(AF_UNIX, SOCK_STREAM, 0);
345 [ # # ]: 0 : if (fd < 0)
346 : : return -1;
347 [ # # ]: 0 : VHOST_CONFIG_LOG(vsocket->path, INFO, "vhost-user %s: socket created, fd: %d",
348 : : vsocket->is_server ? "server" : "client", fd);
349 : :
350 [ # # # # ]: 0 : if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
351 : 0 : VHOST_CONFIG_LOG(vsocket->path, ERR,
352 : : "vhost-user: can't set nonblocking mode for socket, fd: %d (%s)",
353 : : fd, strerror(errno));
354 : 0 : close(fd);
355 : 0 : return -1;
356 : : }
357 : :
358 : : memset(un, 0, sizeof(*un));
359 : 0 : un->sun_family = AF_UNIX;
360 : 0 : strlcpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
361 : :
362 : 0 : vsocket->socket_fd = fd;
363 : 0 : return 0;
364 : : }
365 : :
366 : : static int
367 : 0 : vhost_user_start_server(struct vhost_user_socket *vsocket)
368 : : {
369 : : int ret;
370 : 0 : int fd = vsocket->socket_fd;
371 : 0 : const char *path = vsocket->path;
372 : :
373 : : /*
374 : : * bind () may fail if the socket file with the same name already
375 : : * exists. But the library obviously should not delete the file
376 : : * provided by the user, since we can not be sure that it is not
377 : : * being used by other applications. Moreover, many applications form
378 : : * socket names based on user input, which is prone to errors.
379 : : *
380 : : * The user must ensure that the socket does not exist before
381 : : * registering the vhost driver in server mode.
382 : : */
383 : 0 : ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
384 [ # # ]: 0 : if (ret < 0) {
385 : 0 : VHOST_CONFIG_LOG(path, ERR, "failed to bind: %s; remove it and try again",
386 : : strerror(errno));
387 : 0 : goto err;
388 : : }
389 : 0 : VHOST_CONFIG_LOG(path, INFO, "binding succeeded");
390 : :
391 : 0 : ret = listen(fd, MAX_VIRTIO_BACKLOG);
392 [ # # ]: 0 : if (ret < 0)
393 : 0 : goto err;
394 : :
395 : 0 : ret = fdset_add(vhost_user.fdset, fd, vhost_user_server_new_connection,
396 : : NULL, vsocket);
397 [ # # ]: 0 : if (ret < 0) {
398 : 0 : VHOST_CONFIG_LOG(path, ERR, "failed to add listen fd %d to vhost server fdset",
399 : : fd);
400 : 0 : goto err;
401 : : }
402 : :
403 : : return 0;
404 : :
405 : 0 : err:
406 : 0 : close(fd);
407 : 0 : return -1;
408 : : }
409 : :
410 : : struct vhost_user_reconnect {
411 : : struct sockaddr_un un;
412 : : int fd;
413 : : struct vhost_user_socket *vsocket;
414 : :
415 : : TAILQ_ENTRY(vhost_user_reconnect) next;
416 : : };
417 : :
418 : : TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
419 : : struct vhost_user_reconnect_list {
420 : : struct vhost_user_reconnect_tailq_list head;
421 : : pthread_mutex_t mutex;
422 : : };
423 : :
424 : : static struct vhost_user_reconnect_list reconn_list;
425 : : static rte_thread_t reconn_tid;
426 : :
427 : : static int
428 : 0 : vhost_user_connect_nonblock(char *path, int fd, struct sockaddr *un, size_t sz)
429 : : {
430 : : int ret, flags;
431 : :
432 : 0 : ret = connect(fd, un, sz);
433 [ # # # # ]: 0 : if (ret < 0 && errno != EISCONN)
434 : : return -1;
435 : :
436 : 0 : flags = fcntl(fd, F_GETFL, 0);
437 [ # # ]: 0 : if (flags < 0) {
438 : 0 : VHOST_CONFIG_LOG(path, ERR, "can't get flags for connfd %d (%s)",
439 : : fd, strerror(errno));
440 : 0 : return -2;
441 : : }
442 [ # # # # ]: 0 : if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
443 : 0 : VHOST_CONFIG_LOG(path, ERR, "can't disable nonblocking on fd %d", fd);
444 : 0 : return -2;
445 : : }
446 : : return 0;
447 : : }
448 : :
449 : : static uint32_t
450 : 0 : vhost_user_client_reconnect(void *arg __rte_unused)
451 : : {
452 : : int ret;
453 : : struct vhost_user_reconnect *reconn, *next;
454 : :
455 : : while (1) {
456 : 0 : pthread_mutex_lock(&reconn_list.mutex);
457 : :
458 : : /*
459 : : * An equal implementation of TAILQ_FOREACH_SAFE,
460 : : * which does not exist on all platforms.
461 : : */
462 : 0 : for (reconn = TAILQ_FIRST(&reconn_list.head);
463 [ # # ]: 0 : reconn != NULL; reconn = next) {
464 : 0 : next = TAILQ_NEXT(reconn, next);
465 : :
466 : 0 : ret = vhost_user_connect_nonblock(reconn->vsocket->path, reconn->fd,
467 : 0 : (struct sockaddr *)&reconn->un,
468 : : sizeof(reconn->un));
469 [ # # ]: 0 : if (ret == -2) {
470 : 0 : close(reconn->fd);
471 : 0 : VHOST_CONFIG_LOG(reconn->vsocket->path, ERR,
472 : : "reconnection for fd %d failed",
473 : : reconn->fd);
474 : 0 : goto remove_fd;
475 : : }
476 [ # # ]: 0 : if (ret == -1)
477 : 0 : continue;
478 : :
479 : 0 : VHOST_CONFIG_LOG(reconn->vsocket->path, INFO, "connected");
480 : 0 : vhost_user_add_connection(reconn->fd, reconn->vsocket);
481 : 0 : remove_fd:
482 [ # # ]: 0 : TAILQ_REMOVE(&reconn_list.head, reconn, next);
483 : 0 : free(reconn);
484 : : }
485 : :
486 : 0 : pthread_mutex_unlock(&reconn_list.mutex);
487 : 0 : sleep(1);
488 : : }
489 : :
490 : : return 0;
491 : : }
492 : :
493 : : static int
494 : 0 : vhost_user_reconnect_init(void)
495 : : {
496 : : int ret;
497 : :
498 : 0 : pthread_mutex_init(&reconn_list.mutex, NULL);
499 : 0 : TAILQ_INIT(&reconn_list.head);
500 : :
501 : 0 : ret = rte_thread_create_internal_control(&reconn_tid, "vhost-reco",
502 : : vhost_user_client_reconnect, NULL);
503 [ # # ]: 0 : if (ret != 0) {
504 : 0 : VHOST_CONFIG_LOG("thread", ERR, "failed to create reconnect thread");
505 [ # # ]: 0 : if (pthread_mutex_destroy(&reconn_list.mutex))
506 : 0 : VHOST_CONFIG_LOG("thread", ERR,
507 : : "%s: failed to destroy reconnect mutex",
508 : : __func__);
509 : : }
510 : :
511 : 0 : return ret;
512 : : }
513 : :
514 : : static int
515 : 0 : vhost_user_start_client(struct vhost_user_socket *vsocket)
516 : : {
517 : : int ret;
518 : 0 : int fd = vsocket->socket_fd;
519 : 0 : const char *path = vsocket->path;
520 : : struct vhost_user_reconnect *reconn;
521 : :
522 [ # # # # ]: 0 : if (!vsocket->async_connect || !vsocket->reconnect) {
523 : 0 : ret = vhost_user_connect_nonblock(vsocket->path, fd,
524 : 0 : (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
525 [ # # ]: 0 : if (ret == 0) {
526 : 0 : vhost_user_add_connection(fd, vsocket);
527 : 0 : return 0;
528 : : }
529 : :
530 : 0 : VHOST_CONFIG_LOG(path, WARNING, "failed to connect: %s", strerror(errno));
531 : :
532 [ # # # # ]: 0 : if (ret == -2 || !vsocket->reconnect) {
533 : 0 : close(fd);
534 : 0 : return -1;
535 : : }
536 : :
537 : 0 : VHOST_CONFIG_LOG(path, INFO, "reconnecting...");
538 : : }
539 : 0 : reconn = malloc(sizeof(*reconn));
540 [ # # ]: 0 : if (reconn == NULL) {
541 : 0 : VHOST_CONFIG_LOG(path, ERR, "failed to allocate memory for reconnect");
542 : 0 : close(fd);
543 : 0 : return -1;
544 : : }
545 : 0 : reconn->un = vsocket->un;
546 : 0 : reconn->fd = fd;
547 : 0 : reconn->vsocket = vsocket;
548 : 0 : pthread_mutex_lock(&reconn_list.mutex);
549 : 0 : TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
550 : 0 : pthread_mutex_unlock(&reconn_list.mutex);
551 : :
552 : 0 : return 0;
553 : : }
554 : :
555 : : static struct vhost_user_socket *
556 : 0 : find_vhost_user_socket(const char *path)
557 : : {
558 : : int i;
559 : :
560 [ # # ]: 0 : if (path == NULL)
561 : : return NULL;
562 : :
563 [ # # ]: 0 : for (i = 0; i < vhost_user.vsocket_cnt; i++) {
564 : 0 : struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
565 : :
566 [ # # ]: 0 : if (!strcmp(vsocket->path, path))
567 : 0 : return vsocket;
568 : : }
569 : :
570 : : return NULL;
571 : : }
572 : :
573 : : RTE_EXPORT_SYMBOL(rte_vhost_driver_attach_vdpa_device)
574 : : int
575 : 0 : rte_vhost_driver_attach_vdpa_device(const char *path,
576 : : struct rte_vdpa_device *dev)
577 : : {
578 : : struct vhost_user_socket *vsocket;
579 : :
580 [ # # ]: 0 : if (dev == NULL || path == NULL)
581 : : return -1;
582 : :
583 : 0 : pthread_mutex_lock(&vhost_user.mutex);
584 : 0 : vsocket = find_vhost_user_socket(path);
585 [ # # ]: 0 : if (vsocket)
586 : 0 : vsocket->vdpa_dev = dev;
587 : 0 : pthread_mutex_unlock(&vhost_user.mutex);
588 : :
589 [ # # ]: 0 : return vsocket ? 0 : -1;
590 : : }
591 : :
592 : : RTE_EXPORT_SYMBOL(rte_vhost_driver_detach_vdpa_device)
593 : : int
594 : 0 : rte_vhost_driver_detach_vdpa_device(const char *path)
595 : : {
596 : : struct vhost_user_socket *vsocket;
597 : :
598 : 0 : pthread_mutex_lock(&vhost_user.mutex);
599 : 0 : vsocket = find_vhost_user_socket(path);
600 [ # # ]: 0 : if (vsocket)
601 : 0 : vsocket->vdpa_dev = NULL;
602 : 0 : pthread_mutex_unlock(&vhost_user.mutex);
603 : :
604 [ # # ]: 0 : return vsocket ? 0 : -1;
605 : : }
606 : :
607 : : RTE_EXPORT_SYMBOL(rte_vhost_driver_get_vdpa_device)
608 : : struct rte_vdpa_device *
609 : 0 : rte_vhost_driver_get_vdpa_device(const char *path)
610 : : {
611 : : struct vhost_user_socket *vsocket;
612 : : struct rte_vdpa_device *dev = NULL;
613 : :
614 : 0 : pthread_mutex_lock(&vhost_user.mutex);
615 : 0 : vsocket = find_vhost_user_socket(path);
616 [ # # ]: 0 : if (vsocket)
617 : 0 : dev = vsocket->vdpa_dev;
618 : 0 : pthread_mutex_unlock(&vhost_user.mutex);
619 : :
620 : 0 : return dev;
621 : : }
622 : :
623 : : RTE_EXPORT_SYMBOL(rte_vhost_driver_get_vdpa_dev_type)
624 : : int
625 : 0 : rte_vhost_driver_get_vdpa_dev_type(const char *path, uint32_t *type)
626 : : {
627 : : struct vhost_user_socket *vsocket;
628 : : struct rte_vdpa_device *vdpa_dev;
629 : : int ret = 0;
630 : :
631 : 0 : pthread_mutex_lock(&vhost_user.mutex);
632 : 0 : vsocket = find_vhost_user_socket(path);
633 [ # # ]: 0 : if (!vsocket) {
634 : 0 : VHOST_CONFIG_LOG(path, ERR, "socket file is not registered yet.");
635 : : ret = -1;
636 : 0 : goto unlock_exit;
637 : : }
638 : :
639 : 0 : vdpa_dev = vsocket->vdpa_dev;
640 [ # # ]: 0 : if (!vdpa_dev) {
641 : : ret = -1;
642 : 0 : goto unlock_exit;
643 : : }
644 : :
645 : 0 : *type = vdpa_dev->type;
646 : :
647 : 0 : unlock_exit:
648 : 0 : pthread_mutex_unlock(&vhost_user.mutex);
649 : 0 : return ret;
650 : : }
651 : :
652 : : RTE_EXPORT_SYMBOL(rte_vhost_driver_disable_features)
653 : : int
654 : 0 : rte_vhost_driver_disable_features(const char *path, uint64_t features)
655 : : {
656 : : struct vhost_user_socket *vsocket;
657 : :
658 : 0 : pthread_mutex_lock(&vhost_user.mutex);
659 : 0 : vsocket = find_vhost_user_socket(path);
660 : :
661 : : /* Note that use_builtin_virtio_net is not affected by this function
662 : : * since callers may want to selectively disable features of the
663 : : * built-in vhost net device backend.
664 : : */
665 : :
666 [ # # ]: 0 : if (vsocket)
667 : 0 : vsocket->features &= ~features;
668 : 0 : pthread_mutex_unlock(&vhost_user.mutex);
669 : :
670 [ # # ]: 0 : return vsocket ? 0 : -1;
671 : : }
672 : :
673 : : RTE_EXPORT_SYMBOL(rte_vhost_driver_enable_features)
674 : : int
675 : 0 : rte_vhost_driver_enable_features(const char *path, uint64_t features)
676 : : {
677 : : struct vhost_user_socket *vsocket;
678 : :
679 : 0 : pthread_mutex_lock(&vhost_user.mutex);
680 : 0 : vsocket = find_vhost_user_socket(path);
681 [ # # ]: 0 : if (vsocket) {
682 [ # # ]: 0 : if ((vsocket->supported_features & features) != features) {
683 : : /*
684 : : * trying to enable features the driver doesn't
685 : : * support.
686 : : */
687 : 0 : pthread_mutex_unlock(&vhost_user.mutex);
688 : 0 : return -1;
689 : : }
690 : 0 : vsocket->features |= features;
691 : : }
692 : 0 : pthread_mutex_unlock(&vhost_user.mutex);
693 : :
694 [ # # ]: 0 : return vsocket ? 0 : -1;
695 : : }
696 : :
697 : : RTE_EXPORT_SYMBOL(rte_vhost_driver_set_features)
698 : : int
699 : 0 : rte_vhost_driver_set_features(const char *path, uint64_t features)
700 : : {
701 : : struct vhost_user_socket *vsocket;
702 : :
703 : 0 : pthread_mutex_lock(&vhost_user.mutex);
704 : 0 : vsocket = find_vhost_user_socket(path);
705 [ # # ]: 0 : if (vsocket) {
706 : 0 : vsocket->supported_features = features;
707 : 0 : vsocket->features = features;
708 : :
709 : : /* Anyone setting feature bits is implementing their own vhost
710 : : * device backend.
711 : : */
712 : 0 : vsocket->use_builtin_virtio_net = false;
713 : : }
714 : 0 : pthread_mutex_unlock(&vhost_user.mutex);
715 : :
716 [ # # ]: 0 : return vsocket ? 0 : -1;
717 : : }
718 : :
719 : : RTE_EXPORT_SYMBOL(rte_vhost_driver_get_features)
720 : : int
721 : 0 : rte_vhost_driver_get_features(const char *path, uint64_t *features)
722 : : {
723 : : struct vhost_user_socket *vsocket;
724 : : uint64_t vdpa_features;
725 : : struct rte_vdpa_device *vdpa_dev;
726 : : int ret = 0;
727 : :
728 : 0 : pthread_mutex_lock(&vhost_user.mutex);
729 : 0 : vsocket = find_vhost_user_socket(path);
730 [ # # ]: 0 : if (!vsocket) {
731 : 0 : VHOST_CONFIG_LOG(path, ERR, "socket file is not registered yet.");
732 : : ret = -1;
733 : 0 : goto unlock_exit;
734 : : }
735 : :
736 : 0 : vdpa_dev = vsocket->vdpa_dev;
737 [ # # ]: 0 : if (!vdpa_dev) {
738 : 0 : *features = vsocket->features;
739 : 0 : goto unlock_exit;
740 : : }
741 : :
742 [ # # ]: 0 : if (vdpa_dev->ops->get_features(vdpa_dev, &vdpa_features) < 0) {
743 : 0 : VHOST_CONFIG_LOG(path, ERR, "failed to get vdpa features for socket file.");
744 : : ret = -1;
745 : 0 : goto unlock_exit;
746 : : }
747 : :
748 : 0 : *features = vsocket->features & vdpa_features;
749 : :
750 : 0 : unlock_exit:
751 : 0 : pthread_mutex_unlock(&vhost_user.mutex);
752 : 0 : return ret;
753 : : }
754 : :
755 : : RTE_EXPORT_SYMBOL(rte_vhost_driver_set_protocol_features)
756 : : int
757 : 0 : rte_vhost_driver_set_protocol_features(const char *path,
758 : : uint64_t protocol_features)
759 : : {
760 : : struct vhost_user_socket *vsocket;
761 : :
762 : 0 : pthread_mutex_lock(&vhost_user.mutex);
763 : 0 : vsocket = find_vhost_user_socket(path);
764 [ # # ]: 0 : if (vsocket)
765 : 0 : vsocket->protocol_features = protocol_features;
766 : 0 : pthread_mutex_unlock(&vhost_user.mutex);
767 [ # # ]: 0 : return vsocket ? 0 : -1;
768 : : }
769 : :
770 : : RTE_EXPORT_SYMBOL(rte_vhost_driver_get_protocol_features)
771 : : int
772 : 0 : rte_vhost_driver_get_protocol_features(const char *path,
773 : : uint64_t *protocol_features)
774 : : {
775 : : struct vhost_user_socket *vsocket;
776 : : uint64_t vdpa_protocol_features;
777 : : struct rte_vdpa_device *vdpa_dev;
778 : : int ret = 0;
779 : :
780 : 0 : pthread_mutex_lock(&vhost_user.mutex);
781 : 0 : vsocket = find_vhost_user_socket(path);
782 [ # # ]: 0 : if (!vsocket) {
783 : 0 : VHOST_CONFIG_LOG(path, ERR, "socket file is not registered yet.");
784 : : ret = -1;
785 : 0 : goto unlock_exit;
786 : : }
787 : :
788 : 0 : vdpa_dev = vsocket->vdpa_dev;
789 [ # # ]: 0 : if (!vdpa_dev) {
790 : 0 : *protocol_features = vsocket->protocol_features;
791 : 0 : goto unlock_exit;
792 : : }
793 : :
794 [ # # ]: 0 : if (vdpa_dev->ops->get_protocol_features(vdpa_dev,
795 : : &vdpa_protocol_features) < 0) {
796 : 0 : VHOST_CONFIG_LOG(path, ERR, "failed to get vdpa protocol features.");
797 : : ret = -1;
798 : 0 : goto unlock_exit;
799 : : }
800 : :
801 : 0 : *protocol_features = vsocket->protocol_features
802 : 0 : & vdpa_protocol_features;
803 : :
804 : 0 : unlock_exit:
805 : 0 : pthread_mutex_unlock(&vhost_user.mutex);
806 : 0 : return ret;
807 : : }
808 : :
809 : : RTE_EXPORT_SYMBOL(rte_vhost_driver_get_queue_num)
810 : : int
811 : 0 : rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num)
812 : : {
813 : : struct vhost_user_socket *vsocket;
814 : : uint32_t vdpa_queue_num;
815 : : struct rte_vdpa_device *vdpa_dev;
816 : : int ret = 0;
817 : :
818 : 0 : pthread_mutex_lock(&vhost_user.mutex);
819 : 0 : vsocket = find_vhost_user_socket(path);
820 [ # # ]: 0 : if (!vsocket) {
821 : 0 : VHOST_CONFIG_LOG(path, ERR, "socket file is not registered yet.");
822 : : ret = -1;
823 : 0 : goto unlock_exit;
824 : : }
825 : :
826 : 0 : vdpa_dev = vsocket->vdpa_dev;
827 [ # # ]: 0 : if (!vdpa_dev) {
828 : 0 : *queue_num = vsocket->max_queue_pairs;
829 : 0 : goto unlock_exit;
830 : : }
831 : :
832 [ # # ]: 0 : if (vdpa_dev->ops->get_queue_num(vdpa_dev, &vdpa_queue_num) < 0) {
833 : 0 : VHOST_CONFIG_LOG(path, ERR, "failed to get vdpa queue number.");
834 : : ret = -1;
835 : 0 : goto unlock_exit;
836 : : }
837 : :
838 : 0 : *queue_num = RTE_MIN(vsocket->max_queue_pairs, vdpa_queue_num);
839 : :
840 : 0 : unlock_exit:
841 : 0 : pthread_mutex_unlock(&vhost_user.mutex);
842 : 0 : return ret;
843 : : }
844 : :
845 : : RTE_EXPORT_SYMBOL(rte_vhost_driver_set_max_queue_num)
846 : : int
847 : 0 : rte_vhost_driver_set_max_queue_num(const char *path, uint32_t max_queue_pairs)
848 : : {
849 : : struct vhost_user_socket *vsocket;
850 : : int ret = 0;
851 : :
852 : 0 : pthread_mutex_lock(&vhost_user.mutex);
853 : 0 : vsocket = find_vhost_user_socket(path);
854 [ # # ]: 0 : if (!vsocket) {
855 : 0 : VHOST_CONFIG_LOG(path, ERR, "socket file is not registered yet.");
856 : : ret = -1;
857 : 0 : goto unlock_exit;
858 : : }
859 : :
860 : : /*
861 : : * This is only useful for VDUSE for which number of virtqueues is set
862 : : * by the backend. For Vhost-user, the number of virtqueues is defined
863 : : * by the frontend.
864 : : */
865 [ # # ]: 0 : if (!vsocket->is_vduse) {
866 : 0 : VHOST_CONFIG_LOG(path, DEBUG,
867 : : "Keeping %u max queue pairs for Vhost-user backend",
868 : : VHOST_MAX_QUEUE_PAIRS);
869 : 0 : goto unlock_exit;
870 : : }
871 : :
872 : 0 : VHOST_CONFIG_LOG(path, INFO, "Setting max queue pairs to %u", max_queue_pairs);
873 : :
874 [ # # ]: 0 : if (max_queue_pairs > VHOST_MAX_QUEUE_PAIRS) {
875 : 0 : VHOST_CONFIG_LOG(path, ERR, "Library only supports up to %u queue pairs",
876 : : VHOST_MAX_QUEUE_PAIRS);
877 : : ret = -1;
878 : 0 : goto unlock_exit;
879 : : }
880 : :
881 : 0 : vsocket->max_queue_pairs = max_queue_pairs;
882 : :
883 : 0 : unlock_exit:
884 : 0 : pthread_mutex_unlock(&vhost_user.mutex);
885 : 0 : return ret;
886 : : }
887 : :
888 : : static void
889 : 0 : vhost_user_socket_mem_free(struct vhost_user_socket *vsocket)
890 : : {
891 [ # # ]: 0 : if (vsocket == NULL)
892 : : return;
893 : :
894 : 0 : free(vsocket->path);
895 : 0 : free(vsocket);
896 : : }
897 : :
898 : : /*
899 : : * Register a new vhost-user socket; here we could act as server
900 : : * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
901 : : * is set.
902 : : */
903 : : RTE_EXPORT_SYMBOL(rte_vhost_driver_register)
904 : : int
905 : 0 : rte_vhost_driver_register(const char *path, uint64_t flags)
906 : : {
907 : : struct vhost_user_socket *vsocket;
908 : :
909 [ # # ]: 0 : if (!path)
910 : : return -1;
911 : :
912 : 0 : pthread_mutex_lock(&vhost_user.mutex);
913 : :
914 [ # # ]: 0 : if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
915 : 0 : VHOST_CONFIG_LOG(path, ERR, "the number of vhost sockets reaches maximum");
916 : 0 : goto out;
917 : : }
918 : :
919 : 0 : vsocket = malloc(sizeof(struct vhost_user_socket));
920 [ # # ]: 0 : if (!vsocket)
921 : 0 : goto out;
922 : : memset(vsocket, 0, sizeof(struct vhost_user_socket));
923 : 0 : vsocket->path = strdup(path);
924 [ # # ]: 0 : if (vsocket->path == NULL) {
925 : 0 : VHOST_CONFIG_LOG(path, ERR, "failed to copy socket path string");
926 : 0 : vhost_user_socket_mem_free(vsocket);
927 : 0 : goto out;
928 : : }
929 : 0 : TAILQ_INIT(&vsocket->conn_list);
930 : 0 : pthread_mutex_init(&vsocket->conn_mutex, NULL);
931 : :
932 [ # # ]: 0 : if (!strncmp("/dev/vduse/", path, strlen("/dev/vduse/")))
933 : 0 : vsocket->is_vduse = true;
934 : :
935 : 0 : vsocket->vdpa_dev = NULL;
936 : 0 : vsocket->max_queue_pairs = VHOST_MAX_QUEUE_PAIRS;
937 : 0 : vsocket->extbuf = flags & RTE_VHOST_USER_EXTBUF_SUPPORT;
938 : 0 : vsocket->linearbuf = flags & RTE_VHOST_USER_LINEARBUF_SUPPORT;
939 : 0 : vsocket->async_copy = flags & RTE_VHOST_USER_ASYNC_COPY;
940 : 0 : vsocket->net_compliant_ol_flags = flags & RTE_VHOST_USER_NET_COMPLIANT_OL_FLAGS;
941 : 0 : vsocket->stats_enabled = flags & RTE_VHOST_USER_NET_STATS_ENABLE;
942 : 0 : vsocket->async_connect = flags & RTE_VHOST_USER_ASYNC_CONNECT;
943 [ # # ]: 0 : if (vsocket->is_vduse)
944 : 0 : vsocket->iommu_support = true;
945 : : else
946 : 0 : vsocket->iommu_support = flags & RTE_VHOST_USER_IOMMU_SUPPORT;
947 : :
948 [ # # # # ]: 0 : if (vsocket->async_copy && (vsocket->iommu_support ||
949 [ # # ]: 0 : (flags & RTE_VHOST_USER_POSTCOPY_SUPPORT))) {
950 : 0 : VHOST_CONFIG_LOG(path, ERR, "async copy with IOMMU or post-copy not supported");
951 : 0 : goto out_mutex;
952 : : }
953 : :
954 : : /*
955 : : * Set the supported features correctly for the builtin vhost-user
956 : : * net driver.
957 : : *
958 : : * Applications know nothing about features the builtin virtio net
959 : : * driver (virtio_net.c) supports, thus it's not possible for them
960 : : * to invoke rte_vhost_driver_set_features(). To workaround it, here
961 : : * we set it unconditionally. If the application want to implement
962 : : * another vhost-user driver (say SCSI), it should call the
963 : : * rte_vhost_driver_set_features(), which will overwrite following
964 : : * two values.
965 : : */
966 : 0 : vsocket->use_builtin_virtio_net = true;
967 [ # # ]: 0 : if (vsocket->is_vduse) {
968 : 0 : vsocket->supported_features = VDUSE_NET_SUPPORTED_FEATURES;
969 : 0 : vsocket->features = VDUSE_NET_SUPPORTED_FEATURES;
970 : : } else {
971 : 0 : vsocket->supported_features = VHOST_USER_NET_SUPPORTED_FEATURES;
972 : 0 : vsocket->features = VHOST_USER_NET_SUPPORTED_FEATURES;
973 : 0 : vsocket->protocol_features = VHOST_USER_PROTOCOL_FEATURES;
974 : : }
975 : :
976 [ # # ]: 0 : if (vsocket->async_copy) {
977 : 0 : vsocket->supported_features &= ~(1ULL << VHOST_F_LOG_ALL);
978 : 0 : vsocket->features &= ~(1ULL << VHOST_F_LOG_ALL);
979 : 0 : VHOST_CONFIG_LOG(path, INFO, "logging feature is disabled in async copy mode");
980 : : }
981 : :
982 : : /*
983 : : * We'll not be able to receive a buffer from guest in linear mode
984 : : * without external buffer if it will not fit in a single mbuf, which is
985 : : * likely if segmentation offloading enabled.
986 : : */
987 [ # # # # ]: 0 : if (vsocket->linearbuf && !vsocket->extbuf) {
988 : : uint64_t seg_offload_features =
989 : : (1ULL << VIRTIO_NET_F_HOST_TSO4) |
990 : : (1ULL << VIRTIO_NET_F_HOST_TSO6) |
991 : : (1ULL << VIRTIO_NET_F_HOST_UFO);
992 : :
993 : 0 : VHOST_CONFIG_LOG(path, INFO, "Linear buffers requested without external buffers,");
994 : 0 : VHOST_CONFIG_LOG(path, INFO, "disabling host segmentation offloading support");
995 : 0 : vsocket->supported_features &= ~seg_offload_features;
996 : 0 : vsocket->features &= ~seg_offload_features;
997 : : }
998 : :
999 [ # # ]: 0 : if (!vsocket->iommu_support) {
1000 : 0 : vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
1001 : 0 : vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
1002 : : }
1003 : :
1004 [ # # ]: 0 : if (!(flags & RTE_VHOST_USER_POSTCOPY_SUPPORT)) {
1005 : 0 : vsocket->protocol_features &=
1006 : : ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
1007 : : } else {
1008 : : #ifndef RTE_LIBRTE_VHOST_POSTCOPY
1009 : : VHOST_CONFIG_LOG(path, ERR, "Postcopy requested but not compiled");
1010 : : goto out_mutex;
1011 : : #endif
1012 : : }
1013 : :
1014 [ # # ]: 0 : if (!vsocket->is_vduse) {
1015 [ # # ]: 0 : if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
1016 : 0 : vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
1017 [ # # # # ]: 0 : if (vsocket->reconnect && reconn_tid.opaque_id == 0) {
1018 [ # # ]: 0 : if (vhost_user_reconnect_init() != 0)
1019 : 0 : goto out_mutex;
1020 : : }
1021 : : } else {
1022 : 0 : vsocket->is_server = true;
1023 : : }
1024 [ # # ]: 0 : if (create_unix_socket(vsocket) < 0)
1025 : 0 : goto out_mutex;
1026 : : }
1027 : :
1028 : 0 : vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
1029 : :
1030 : 0 : pthread_mutex_unlock(&vhost_user.mutex);
1031 : 0 : return 0;
1032 : :
1033 : 0 : out_mutex:
1034 [ # # ]: 0 : if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
1035 : 0 : VHOST_CONFIG_LOG(path, ERR, "failed to destroy connection mutex");
1036 : : }
1037 : 0 : vhost_user_socket_mem_free(vsocket);
1038 : 0 : out:
1039 : 0 : pthread_mutex_unlock(&vhost_user.mutex);
1040 : 0 : return -1;
1041 : : }
1042 : :
1043 : : static bool
1044 : 0 : vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
1045 : : {
1046 : : int found = false;
1047 : : struct vhost_user_reconnect *reconn, *next;
1048 : :
1049 : 0 : pthread_mutex_lock(&reconn_list.mutex);
1050 : :
1051 : 0 : for (reconn = TAILQ_FIRST(&reconn_list.head);
1052 [ # # ]: 0 : reconn != NULL; reconn = next) {
1053 : 0 : next = TAILQ_NEXT(reconn, next);
1054 : :
1055 [ # # ]: 0 : if (reconn->vsocket == vsocket) {
1056 [ # # ]: 0 : TAILQ_REMOVE(&reconn_list.head, reconn, next);
1057 : 0 : close(reconn->fd);
1058 : 0 : free(reconn);
1059 : : found = true;
1060 : 0 : break;
1061 : : }
1062 : : }
1063 : 0 : pthread_mutex_unlock(&reconn_list.mutex);
1064 : 0 : return found;
1065 : : }
1066 : :
1067 : : /**
1068 : : * Unregister the specified vhost socket
1069 : : */
1070 : : RTE_EXPORT_SYMBOL(rte_vhost_driver_unregister)
1071 : : int
1072 : 0 : rte_vhost_driver_unregister(const char *path)
1073 : : {
1074 : : int i;
1075 : : int count;
1076 : : struct vhost_user_connection *conn, *next;
1077 : :
1078 [ # # ]: 0 : if (path == NULL)
1079 : : return -1;
1080 : :
1081 : 0 : again:
1082 : 0 : pthread_mutex_lock(&vhost_user.mutex);
1083 : :
1084 [ # # ]: 0 : for (i = 0; i < vhost_user.vsocket_cnt; i++) {
1085 : 0 : struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
1086 [ # # ]: 0 : if (strcmp(vsocket->path, path))
1087 : : continue;
1088 : :
1089 [ # # ]: 0 : if (vsocket->is_vduse) {
1090 : 0 : vduse_device_destroy(path);
1091 [ # # ]: 0 : } else if (vsocket->is_server) {
1092 : : /*
1093 : : * If r/wcb is executing, release vhost_user's
1094 : : * mutex lock, and try again since the r/wcb
1095 : : * may use the mutex lock.
1096 : : */
1097 [ # # ]: 0 : if (fdset_try_del(vhost_user.fdset, vsocket->socket_fd) == -1) {
1098 : 0 : pthread_mutex_unlock(&vhost_user.mutex);
1099 : 0 : goto again;
1100 : : }
1101 [ # # ]: 0 : } else if (vsocket->reconnect) {
1102 : 0 : vhost_user_remove_reconnect(vsocket);
1103 : : }
1104 : :
1105 : 0 : pthread_mutex_lock(&vsocket->conn_mutex);
1106 : 0 : for (conn = TAILQ_FIRST(&vsocket->conn_list);
1107 [ # # ]: 0 : conn != NULL;
1108 : : conn = next) {
1109 : 0 : next = TAILQ_NEXT(conn, next);
1110 : :
1111 : : /*
1112 : : * If r/wcb is executing, release vsocket's
1113 : : * conn_mutex and vhost_user's mutex locks, and
1114 : : * try again since the r/wcb may use the
1115 : : * conn_mutex and mutex locks.
1116 : : */
1117 [ # # ]: 0 : if (fdset_try_del(vhost_user.fdset,
1118 : : conn->connfd) == -1) {
1119 : 0 : pthread_mutex_unlock(&vsocket->conn_mutex);
1120 : 0 : pthread_mutex_unlock(&vhost_user.mutex);
1121 : 0 : goto again;
1122 : : }
1123 : :
1124 : 0 : VHOST_CONFIG_LOG(path, INFO, "free connfd %d", conn->connfd);
1125 : 0 : close(conn->connfd);
1126 : 0 : vhost_destroy_device(conn->vid);
1127 [ # # ]: 0 : TAILQ_REMOVE(&vsocket->conn_list, conn, next);
1128 : 0 : free(conn);
1129 : : }
1130 : 0 : pthread_mutex_unlock(&vsocket->conn_mutex);
1131 : :
1132 [ # # ]: 0 : if (vsocket->is_server) {
1133 : 0 : close(vsocket->socket_fd);
1134 : 0 : unlink(path);
1135 : : }
1136 : :
1137 : 0 : pthread_mutex_destroy(&vsocket->conn_mutex);
1138 : 0 : vhost_user_socket_mem_free(vsocket);
1139 : :
1140 : 0 : count = --vhost_user.vsocket_cnt;
1141 : 0 : vhost_user.vsockets[i] = vhost_user.vsockets[count];
1142 : 0 : vhost_user.vsockets[count] = NULL;
1143 : :
1144 : : /* Check if we need to destroy the vhost fdset */
1145 [ # # # # ]: 0 : if (vhost_user.vsocket_cnt == 0 && vhost_user.fdset != NULL) {
1146 : 0 : fdset_destroy(vhost_user.fdset);
1147 : 0 : vhost_user.fdset = NULL;
1148 : : }
1149 : :
1150 : 0 : pthread_mutex_unlock(&vhost_user.mutex);
1151 : 0 : return 0;
1152 : : }
1153 : 0 : pthread_mutex_unlock(&vhost_user.mutex);
1154 : :
1155 : 0 : return -1;
1156 : : }
1157 : :
1158 : : /*
1159 : : * Register ops so that we can add/remove device to data core.
1160 : : */
1161 : : RTE_EXPORT_SYMBOL(rte_vhost_driver_callback_register)
1162 : : int
1163 : 0 : rte_vhost_driver_callback_register(const char *path,
1164 : : struct rte_vhost_device_ops const * const ops)
1165 : : {
1166 : : struct vhost_user_socket *vsocket;
1167 : :
1168 : 0 : pthread_mutex_lock(&vhost_user.mutex);
1169 : 0 : vsocket = find_vhost_user_socket(path);
1170 [ # # ]: 0 : if (vsocket)
1171 : 0 : vsocket->notify_ops = ops;
1172 : 0 : pthread_mutex_unlock(&vhost_user.mutex);
1173 : :
1174 [ # # ]: 0 : return vsocket ? 0 : -1;
1175 : : }
1176 : :
1177 : : struct rte_vhost_device_ops const *
1178 : 0 : vhost_driver_callback_get(const char *path)
1179 : : {
1180 : : struct vhost_user_socket *vsocket;
1181 : :
1182 : 0 : pthread_mutex_lock(&vhost_user.mutex);
1183 : 0 : vsocket = find_vhost_user_socket(path);
1184 : 0 : pthread_mutex_unlock(&vhost_user.mutex);
1185 : :
1186 [ # # ]: 0 : return vsocket ? vsocket->notify_ops : NULL;
1187 : : }
1188 : :
1189 : : RTE_EXPORT_SYMBOL(rte_vhost_driver_start)
1190 : : int
1191 : 0 : rte_vhost_driver_start(const char *path)
1192 : : {
1193 : : struct vhost_user_socket *vsocket;
1194 : :
1195 : 0 : pthread_mutex_lock(&vhost_user.mutex);
1196 : 0 : vsocket = find_vhost_user_socket(path);
1197 : 0 : pthread_mutex_unlock(&vhost_user.mutex);
1198 : :
1199 [ # # ]: 0 : if (!vsocket)
1200 : : return -1;
1201 : :
1202 [ # # ]: 0 : if (vsocket->is_vduse)
1203 : 0 : return vduse_device_create(path, vsocket->net_compliant_ol_flags,
1204 : 0 : vsocket->extbuf, vsocket->linearbuf);
1205 : :
1206 [ # # ]: 0 : if (vhost_user.fdset == NULL) {
1207 : 0 : vhost_user.fdset = fdset_init("vhost-evt");
1208 [ # # ]: 0 : if (vhost_user.fdset == NULL) {
1209 : 0 : VHOST_CONFIG_LOG(path, ERR, "failed to init Vhost-user fdset");
1210 : 0 : return -1;
1211 : : }
1212 : : }
1213 : :
1214 [ # # ]: 0 : if (vsocket->is_server)
1215 : 0 : return vhost_user_start_server(vsocket);
1216 : : else
1217 : 0 : return vhost_user_start_client(vsocket);
1218 : : }
|