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