vhost: fix race condition when adding fd in the fdset
[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;
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;
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:
271         free(conn);
272         close(fd);
273 }
274
275 /* call back when there is new vhost-user connection from client  */
276 static void
277 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
278 {
279         struct vhost_user_socket *vsocket = dat;
280
281         fd = accept(fd, NULL, NULL);
282         if (fd < 0)
283                 return;
284
285         RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
286         vhost_user_add_connection(fd, vsocket);
287 }
288
289 static void
290 vhost_user_read_cb(int connfd, void *dat, int *remove)
291 {
292         struct vhost_user_connection *conn = dat;
293         struct vhost_user_socket *vsocket = conn->vsocket;
294         int ret;
295
296         ret = vhost_user_msg_handler(conn->vid, connfd);
297         if (ret < 0) {
298                 close(connfd);
299                 *remove = 1;
300                 vhost_destroy_device(conn->vid);
301
302                 if (vsocket->notify_ops->destroy_connection)
303                         vsocket->notify_ops->destroy_connection(conn->vid);
304
305                 pthread_mutex_lock(&vsocket->conn_mutex);
306                 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
307                 pthread_mutex_unlock(&vsocket->conn_mutex);
308
309                 free(conn);
310
311                 if (vsocket->reconnect) {
312                         create_unix_socket(vsocket);
313                         vhost_user_start_client(vsocket);
314                 }
315         }
316 }
317
318 static int
319 create_unix_socket(struct vhost_user_socket *vsocket)
320 {
321         int fd;
322         struct sockaddr_un *un = &vsocket->un;
323
324         fd = socket(AF_UNIX, SOCK_STREAM, 0);
325         if (fd < 0)
326                 return -1;
327         RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
328                 vsocket->is_server ? "server" : "client", fd);
329
330         if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
331                 RTE_LOG(ERR, VHOST_CONFIG,
332                         "vhost-user: can't set nonblocking mode for socket, fd: "
333                         "%d (%s)\n", fd, strerror(errno));
334                 close(fd);
335                 return -1;
336         }
337
338         memset(un, 0, sizeof(*un));
339         un->sun_family = AF_UNIX;
340         strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
341         un->sun_path[sizeof(un->sun_path) - 1] = '\0';
342
343         vsocket->socket_fd = fd;
344         return 0;
345 }
346
347 static int
348 vhost_user_start_server(struct vhost_user_socket *vsocket)
349 {
350         int ret;
351         int fd = vsocket->socket_fd;
352         const char *path = vsocket->path;
353
354         /*
355          * bind () may fail if the socket file with the same name already
356          * exists. But the library obviously should not delete the file
357          * provided by the user, since we can not be sure that it is not
358          * being used by other applications. Moreover, many applications form
359          * socket names based on user input, which is prone to errors.
360          *
361          * The user must ensure that the socket does not exist before
362          * registering the vhost driver in server mode.
363          */
364         ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
365         if (ret < 0) {
366                 RTE_LOG(ERR, VHOST_CONFIG,
367                         "failed to bind to %s: %s; remove it and try again\n",
368                         path, strerror(errno));
369                 goto err;
370         }
371         RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
372
373         ret = listen(fd, MAX_VIRTIO_BACKLOG);
374         if (ret < 0)
375                 goto err;
376
377         ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
378                   NULL, vsocket);
379         if (ret < 0) {
380                 RTE_LOG(ERR, VHOST_CONFIG,
381                         "failed to add listen fd %d to vhost server fdset\n",
382                         fd);
383                 goto err;
384         }
385
386         return 0;
387
388 err:
389         close(fd);
390         return -1;
391 }
392
393 struct vhost_user_reconnect {
394         struct sockaddr_un un;
395         int fd;
396         struct vhost_user_socket *vsocket;
397
398         TAILQ_ENTRY(vhost_user_reconnect) next;
399 };
400
401 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
402 struct vhost_user_reconnect_list {
403         struct vhost_user_reconnect_tailq_list head;
404         pthread_mutex_t mutex;
405 };
406
407 static struct vhost_user_reconnect_list reconn_list;
408 static pthread_t reconn_tid;
409
410 static int
411 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
412 {
413         int ret, flags;
414
415         ret = connect(fd, un, sz);
416         if (ret < 0 && errno != EISCONN)
417                 return -1;
418
419         flags = fcntl(fd, F_GETFL, 0);
420         if (flags < 0) {
421                 RTE_LOG(ERR, VHOST_CONFIG,
422                         "can't get flags for connfd %d\n", fd);
423                 return -2;
424         }
425         if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
426                 RTE_LOG(ERR, VHOST_CONFIG,
427                                 "can't disable nonblocking on fd %d\n", fd);
428                 return -2;
429         }
430         return 0;
431 }
432
433 static void *
434 vhost_user_client_reconnect(void *arg __rte_unused)
435 {
436         int ret;
437         struct vhost_user_reconnect *reconn, *next;
438
439         while (1) {
440                 pthread_mutex_lock(&reconn_list.mutex);
441
442                 /*
443                  * An equal implementation of TAILQ_FOREACH_SAFE,
444                  * which does not exist on all platforms.
445                  */
446                 for (reconn = TAILQ_FIRST(&reconn_list.head);
447                      reconn != NULL; reconn = next) {
448                         next = TAILQ_NEXT(reconn, next);
449
450                         ret = vhost_user_connect_nonblock(reconn->fd,
451                                                 (struct sockaddr *)&reconn->un,
452                                                 sizeof(reconn->un));
453                         if (ret == -2) {
454                                 close(reconn->fd);
455                                 RTE_LOG(ERR, VHOST_CONFIG,
456                                         "reconnection for fd %d failed\n",
457                                         reconn->fd);
458                                 goto remove_fd;
459                         }
460                         if (ret == -1)
461                                 continue;
462
463                         RTE_LOG(INFO, VHOST_CONFIG,
464                                 "%s: connected\n", reconn->vsocket->path);
465                         vhost_user_add_connection(reconn->fd, reconn->vsocket);
466 remove_fd:
467                         TAILQ_REMOVE(&reconn_list.head, reconn, next);
468                         free(reconn);
469                 }
470
471                 pthread_mutex_unlock(&reconn_list.mutex);
472                 sleep(1);
473         }
474
475         return NULL;
476 }
477
478 static int
479 vhost_user_reconnect_init(void)
480 {
481         int ret;
482
483         ret = pthread_mutex_init(&reconn_list.mutex, NULL);
484         if (ret < 0) {
485                 RTE_LOG(ERR, VHOST_CONFIG, "failed to initialize mutex");
486                 return ret;
487         }
488         TAILQ_INIT(&reconn_list.head);
489
490         ret = rte_ctrl_thread_create(&reconn_tid, "vhost_reconn", NULL,
491                              vhost_user_client_reconnect, NULL);
492         if (ret != 0) {
493                 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
494                 if (pthread_mutex_destroy(&reconn_list.mutex)) {
495                         RTE_LOG(ERR, VHOST_CONFIG,
496                                 "failed to destroy reconnect mutex");
497                 }
498         }
499
500         return ret;
501 }
502
503 static int
504 vhost_user_start_client(struct vhost_user_socket *vsocket)
505 {
506         int ret;
507         int fd = vsocket->socket_fd;
508         const char *path = vsocket->path;
509         struct vhost_user_reconnect *reconn;
510
511         ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
512                                           sizeof(vsocket->un));
513         if (ret == 0) {
514                 vhost_user_add_connection(fd, vsocket);
515                 return 0;
516         }
517
518         RTE_LOG(WARNING, VHOST_CONFIG,
519                 "failed to connect to %s: %s\n",
520                 path, strerror(errno));
521
522         if (ret == -2 || !vsocket->reconnect) {
523                 close(fd);
524                 return -1;
525         }
526
527         RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
528         reconn = malloc(sizeof(*reconn));
529         if (reconn == NULL) {
530                 RTE_LOG(ERR, VHOST_CONFIG,
531                         "failed to allocate memory for reconnect\n");
532                 close(fd);
533                 return -1;
534         }
535         reconn->un = vsocket->un;
536         reconn->fd = fd;
537         reconn->vsocket = vsocket;
538         pthread_mutex_lock(&reconn_list.mutex);
539         TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
540         pthread_mutex_unlock(&reconn_list.mutex);
541
542         return 0;
543 }
544
545 static struct vhost_user_socket *
546 find_vhost_user_socket(const char *path)
547 {
548         int i;
549
550         for (i = 0; i < vhost_user.vsocket_cnt; i++) {
551                 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
552
553                 if (!strcmp(vsocket->path, path))
554                         return vsocket;
555         }
556
557         return NULL;
558 }
559
560 int
561 rte_vhost_driver_attach_vdpa_device(const char *path, int did)
562 {
563         struct vhost_user_socket *vsocket;
564
565         if (rte_vdpa_get_device(did) == NULL)
566                 return -1;
567
568         pthread_mutex_lock(&vhost_user.mutex);
569         vsocket = find_vhost_user_socket(path);
570         if (vsocket)
571                 vsocket->vdpa_dev_id = did;
572         pthread_mutex_unlock(&vhost_user.mutex);
573
574         return vsocket ? 0 : -1;
575 }
576
577 int
578 rte_vhost_driver_detach_vdpa_device(const char *path)
579 {
580         struct vhost_user_socket *vsocket;
581
582         pthread_mutex_lock(&vhost_user.mutex);
583         vsocket = find_vhost_user_socket(path);
584         if (vsocket)
585                 vsocket->vdpa_dev_id = -1;
586         pthread_mutex_unlock(&vhost_user.mutex);
587
588         return vsocket ? 0 : -1;
589 }
590
591 int
592 rte_vhost_driver_get_vdpa_device_id(const char *path)
593 {
594         struct vhost_user_socket *vsocket;
595         int did = -1;
596
597         pthread_mutex_lock(&vhost_user.mutex);
598         vsocket = find_vhost_user_socket(path);
599         if (vsocket)
600                 did = vsocket->vdpa_dev_id;
601         pthread_mutex_unlock(&vhost_user.mutex);
602
603         return did;
604 }
605
606 int
607 rte_vhost_driver_disable_features(const char *path, uint64_t features)
608 {
609         struct vhost_user_socket *vsocket;
610
611         pthread_mutex_lock(&vhost_user.mutex);
612         vsocket = find_vhost_user_socket(path);
613
614         /* Note that use_builtin_virtio_net is not affected by this function
615          * since callers may want to selectively disable features of the
616          * built-in vhost net device backend.
617          */
618
619         if (vsocket)
620                 vsocket->features &= ~features;
621         pthread_mutex_unlock(&vhost_user.mutex);
622
623         return vsocket ? 0 : -1;
624 }
625
626 int
627 rte_vhost_driver_enable_features(const char *path, uint64_t features)
628 {
629         struct vhost_user_socket *vsocket;
630
631         pthread_mutex_lock(&vhost_user.mutex);
632         vsocket = find_vhost_user_socket(path);
633         if (vsocket) {
634                 if ((vsocket->supported_features & features) != features) {
635                         /*
636                          * trying to enable features the driver doesn't
637                          * support.
638                          */
639                         pthread_mutex_unlock(&vhost_user.mutex);
640                         return -1;
641                 }
642                 vsocket->features |= features;
643         }
644         pthread_mutex_unlock(&vhost_user.mutex);
645
646         return vsocket ? 0 : -1;
647 }
648
649 int
650 rte_vhost_driver_set_features(const char *path, uint64_t features)
651 {
652         struct vhost_user_socket *vsocket;
653
654         pthread_mutex_lock(&vhost_user.mutex);
655         vsocket = find_vhost_user_socket(path);
656         if (vsocket) {
657                 vsocket->supported_features = features;
658                 vsocket->features = features;
659
660                 /* Anyone setting feature bits is implementing their own vhost
661                  * device backend.
662                  */
663                 vsocket->use_builtin_virtio_net = false;
664         }
665         pthread_mutex_unlock(&vhost_user.mutex);
666
667         return vsocket ? 0 : -1;
668 }
669
670 int
671 rte_vhost_driver_get_features(const char *path, uint64_t *features)
672 {
673         struct vhost_user_socket *vsocket;
674         uint64_t vdpa_features;
675         struct rte_vdpa_device *vdpa_dev;
676         int did = -1;
677         int ret = 0;
678
679         pthread_mutex_lock(&vhost_user.mutex);
680         vsocket = find_vhost_user_socket(path);
681         if (!vsocket) {
682                 RTE_LOG(ERR, VHOST_CONFIG,
683                         "socket file %s is not registered yet.\n", path);
684                 ret = -1;
685                 goto unlock_exit;
686         }
687
688         did = vsocket->vdpa_dev_id;
689         vdpa_dev = rte_vdpa_get_device(did);
690         if (!vdpa_dev || !vdpa_dev->ops->get_features) {
691                 *features = vsocket->features;
692                 goto unlock_exit;
693         }
694
695         if (vdpa_dev->ops->get_features(did, &vdpa_features) < 0) {
696                 RTE_LOG(ERR, VHOST_CONFIG,
697                                 "failed to get vdpa features "
698                                 "for socket file %s.\n", path);
699                 ret = -1;
700                 goto unlock_exit;
701         }
702
703         *features = vsocket->features & vdpa_features;
704
705 unlock_exit:
706         pthread_mutex_unlock(&vhost_user.mutex);
707         return ret;
708 }
709
710 int
711 rte_vhost_driver_get_protocol_features(const char *path,
712                 uint64_t *protocol_features)
713 {
714         struct vhost_user_socket *vsocket;
715         uint64_t vdpa_protocol_features;
716         struct rte_vdpa_device *vdpa_dev;
717         int did = -1;
718         int ret = 0;
719
720         pthread_mutex_lock(&vhost_user.mutex);
721         vsocket = find_vhost_user_socket(path);
722         if (!vsocket) {
723                 RTE_LOG(ERR, VHOST_CONFIG,
724                         "socket file %s is not registered yet.\n", path);
725                 ret = -1;
726                 goto unlock_exit;
727         }
728
729         did = vsocket->vdpa_dev_id;
730         vdpa_dev = rte_vdpa_get_device(did);
731         if (!vdpa_dev || !vdpa_dev->ops->get_protocol_features) {
732                 *protocol_features = vsocket->protocol_features;
733                 goto unlock_exit;
734         }
735
736         if (vdpa_dev->ops->get_protocol_features(did,
737                                 &vdpa_protocol_features) < 0) {
738                 RTE_LOG(ERR, VHOST_CONFIG,
739                                 "failed to get vdpa protocol features "
740                                 "for socket file %s.\n", path);
741                 ret = -1;
742                 goto unlock_exit;
743         }
744
745         *protocol_features = vsocket->protocol_features
746                 & vdpa_protocol_features;
747
748 unlock_exit:
749         pthread_mutex_unlock(&vhost_user.mutex);
750         return ret;
751 }
752
753 int
754 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num)
755 {
756         struct vhost_user_socket *vsocket;
757         uint32_t vdpa_queue_num;
758         struct rte_vdpa_device *vdpa_dev;
759         int did = -1;
760         int ret = 0;
761
762         pthread_mutex_lock(&vhost_user.mutex);
763         vsocket = find_vhost_user_socket(path);
764         if (!vsocket) {
765                 RTE_LOG(ERR, VHOST_CONFIG,
766                         "socket file %s is not registered yet.\n", path);
767                 ret = -1;
768                 goto unlock_exit;
769         }
770
771         did = vsocket->vdpa_dev_id;
772         vdpa_dev = rte_vdpa_get_device(did);
773         if (!vdpa_dev || !vdpa_dev->ops->get_queue_num) {
774                 *queue_num = VHOST_MAX_QUEUE_PAIRS;
775                 goto unlock_exit;
776         }
777
778         if (vdpa_dev->ops->get_queue_num(did, &vdpa_queue_num) < 0) {
779                 RTE_LOG(ERR, VHOST_CONFIG,
780                                 "failed to get vdpa queue number "
781                                 "for socket file %s.\n", path);
782                 ret = -1;
783                 goto unlock_exit;
784         }
785
786         *queue_num = RTE_MIN((uint32_t)VHOST_MAX_QUEUE_PAIRS, vdpa_queue_num);
787
788 unlock_exit:
789         pthread_mutex_unlock(&vhost_user.mutex);
790         return ret;
791 }
792
793 static void
794 vhost_user_socket_mem_free(struct vhost_user_socket *vsocket)
795 {
796         if (vsocket && vsocket->path) {
797                 free(vsocket->path);
798                 vsocket->path = NULL;
799         }
800
801         if (vsocket) {
802                 free(vsocket);
803                 vsocket = NULL;
804         }
805 }
806
807 /*
808  * Register a new vhost-user socket; here we could act as server
809  * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
810  * is set.
811  */
812 int
813 rte_vhost_driver_register(const char *path, uint64_t flags)
814 {
815         int ret = -1;
816         struct vhost_user_socket *vsocket;
817
818         if (!path)
819                 return -1;
820
821         pthread_mutex_lock(&vhost_user.mutex);
822
823         if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
824                 RTE_LOG(ERR, VHOST_CONFIG,
825                         "error: the number of vhost sockets reaches maximum\n");
826                 goto out;
827         }
828
829         vsocket = malloc(sizeof(struct vhost_user_socket));
830         if (!vsocket)
831                 goto out;
832         memset(vsocket, 0, sizeof(struct vhost_user_socket));
833         vsocket->path = strdup(path);
834         if (vsocket->path == NULL) {
835                 RTE_LOG(ERR, VHOST_CONFIG,
836                         "error: failed to copy socket path string\n");
837                 vhost_user_socket_mem_free(vsocket);
838                 goto out;
839         }
840         TAILQ_INIT(&vsocket->conn_list);
841         ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
842         if (ret) {
843                 RTE_LOG(ERR, VHOST_CONFIG,
844                         "error: failed to init connection mutex\n");
845                 goto out_free;
846         }
847         vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
848
849         /*
850          * Set the supported features correctly for the builtin vhost-user
851          * net driver.
852          *
853          * Applications know nothing about features the builtin virtio net
854          * driver (virtio_net.c) supports, thus it's not possible for them
855          * to invoke rte_vhost_driver_set_features(). To workaround it, here
856          * we set it unconditionally. If the application want to implement
857          * another vhost-user driver (say SCSI), it should call the
858          * rte_vhost_driver_set_features(), which will overwrite following
859          * two values.
860          */
861         vsocket->use_builtin_virtio_net = true;
862         vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
863         vsocket->features           = VIRTIO_NET_SUPPORTED_FEATURES;
864         vsocket->protocol_features  = VHOST_USER_PROTOCOL_FEATURES;
865
866         /*
867          * Dequeue zero copy can't assure descriptors returned in order.
868          * Also, it requires that the guest memory is populated, which is
869          * not compatible with postcopy.
870          */
871         if (vsocket->dequeue_zero_copy) {
872                 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IN_ORDER);
873                 vsocket->features &= ~(1ULL << VIRTIO_F_IN_ORDER);
874
875                 RTE_LOG(INFO, VHOST_CONFIG,
876                         "Dequeue zero copy requested, disabling postcopy support\n");
877                 vsocket->protocol_features &=
878                         ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
879         }
880
881         if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
882                 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
883                 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
884         }
885
886         if (!(flags & RTE_VHOST_USER_POSTCOPY_SUPPORT)) {
887                 vsocket->protocol_features &=
888                         ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
889         } else {
890 #ifndef RTE_LIBRTE_VHOST_POSTCOPY
891                 RTE_LOG(ERR, VHOST_CONFIG,
892                         "Postcopy requested but not compiled\n");
893                 ret = -1;
894                 goto out_mutex;
895 #endif
896         }
897
898         if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
899                 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
900                 if (vsocket->reconnect && reconn_tid == 0) {
901                         if (vhost_user_reconnect_init() != 0)
902                                 goto out_mutex;
903                 }
904         } else {
905                 vsocket->is_server = true;
906         }
907         ret = create_unix_socket(vsocket);
908         if (ret < 0) {
909                 goto out_mutex;
910         }
911
912         vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
913
914         pthread_mutex_unlock(&vhost_user.mutex);
915         return ret;
916
917 out_mutex:
918         if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
919                 RTE_LOG(ERR, VHOST_CONFIG,
920                         "error: failed to destroy connection mutex\n");
921         }
922 out_free:
923         vhost_user_socket_mem_free(vsocket);
924 out:
925         pthread_mutex_unlock(&vhost_user.mutex);
926
927         return ret;
928 }
929
930 static bool
931 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
932 {
933         int found = false;
934         struct vhost_user_reconnect *reconn, *next;
935
936         pthread_mutex_lock(&reconn_list.mutex);
937
938         for (reconn = TAILQ_FIRST(&reconn_list.head);
939              reconn != NULL; reconn = next) {
940                 next = TAILQ_NEXT(reconn, next);
941
942                 if (reconn->vsocket == vsocket) {
943                         TAILQ_REMOVE(&reconn_list.head, reconn, next);
944                         close(reconn->fd);
945                         free(reconn);
946                         found = true;
947                         break;
948                 }
949         }
950         pthread_mutex_unlock(&reconn_list.mutex);
951         return found;
952 }
953
954 /**
955  * Unregister the specified vhost socket
956  */
957 int
958 rte_vhost_driver_unregister(const char *path)
959 {
960         int i;
961         int count;
962         struct vhost_user_connection *conn, *next;
963
964         pthread_mutex_lock(&vhost_user.mutex);
965
966         for (i = 0; i < vhost_user.vsocket_cnt; i++) {
967                 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
968
969                 if (!strcmp(vsocket->path, path)) {
970 again:
971                         pthread_mutex_lock(&vsocket->conn_mutex);
972                         for (conn = TAILQ_FIRST(&vsocket->conn_list);
973                              conn != NULL;
974                              conn = next) {
975                                 next = TAILQ_NEXT(conn, next);
976
977                                 /*
978                                  * If r/wcb is executing, release the
979                                  * conn_mutex lock, and try again since
980                                  * the r/wcb may use the conn_mutex lock.
981                                  */
982                                 if (fdset_try_del(&vhost_user.fdset,
983                                                   conn->connfd) == -1) {
984                                         pthread_mutex_unlock(
985                                                         &vsocket->conn_mutex);
986                                         goto again;
987                                 }
988
989                                 RTE_LOG(INFO, VHOST_CONFIG,
990                                         "free connfd = %d for device '%s'\n",
991                                         conn->connfd, path);
992                                 close(conn->connfd);
993                                 vhost_destroy_device(conn->vid);
994                                 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
995                                 free(conn);
996                         }
997                         pthread_mutex_unlock(&vsocket->conn_mutex);
998
999                         if (vsocket->is_server) {
1000                                 fdset_del(&vhost_user.fdset,
1001                                                 vsocket->socket_fd);
1002                                 close(vsocket->socket_fd);
1003                                 unlink(path);
1004                         } else if (vsocket->reconnect) {
1005                                 vhost_user_remove_reconnect(vsocket);
1006                         }
1007
1008                         pthread_mutex_destroy(&vsocket->conn_mutex);
1009                         vhost_user_socket_mem_free(vsocket);
1010
1011                         count = --vhost_user.vsocket_cnt;
1012                         vhost_user.vsockets[i] = vhost_user.vsockets[count];
1013                         vhost_user.vsockets[count] = NULL;
1014                         pthread_mutex_unlock(&vhost_user.mutex);
1015
1016                         return 0;
1017                 }
1018         }
1019         pthread_mutex_unlock(&vhost_user.mutex);
1020
1021         return -1;
1022 }
1023
1024 /*
1025  * Register ops so that we can add/remove device to data core.
1026  */
1027 int
1028 rte_vhost_driver_callback_register(const char *path,
1029         struct vhost_device_ops const * const ops)
1030 {
1031         struct vhost_user_socket *vsocket;
1032
1033         pthread_mutex_lock(&vhost_user.mutex);
1034         vsocket = find_vhost_user_socket(path);
1035         if (vsocket)
1036                 vsocket->notify_ops = ops;
1037         pthread_mutex_unlock(&vhost_user.mutex);
1038
1039         return vsocket ? 0 : -1;
1040 }
1041
1042 struct vhost_device_ops const *
1043 vhost_driver_callback_get(const char *path)
1044 {
1045         struct vhost_user_socket *vsocket;
1046
1047         pthread_mutex_lock(&vhost_user.mutex);
1048         vsocket = find_vhost_user_socket(path);
1049         pthread_mutex_unlock(&vhost_user.mutex);
1050
1051         return vsocket ? vsocket->notify_ops : NULL;
1052 }
1053
1054 int
1055 rte_vhost_driver_start(const char *path)
1056 {
1057         struct vhost_user_socket *vsocket;
1058         static pthread_t fdset_tid;
1059
1060         pthread_mutex_lock(&vhost_user.mutex);
1061         vsocket = find_vhost_user_socket(path);
1062         pthread_mutex_unlock(&vhost_user.mutex);
1063
1064         if (!vsocket)
1065                 return -1;
1066
1067         if (fdset_tid == 0) {
1068                 /**
1069                  * create a pipe which will be waited by poll and notified to
1070                  * rebuild the wait list of poll.
1071                  */
1072                 if (fdset_pipe_init(&vhost_user.fdset) < 0) {
1073                         RTE_LOG(ERR, VHOST_CONFIG,
1074                                 "failed to create pipe for vhost fdset\n");
1075                         return -1;
1076                 }
1077
1078                 int ret = rte_ctrl_thread_create(&fdset_tid,
1079                         "vhost-events", NULL, fdset_event_dispatch,
1080                         &vhost_user.fdset);
1081                 if (ret != 0) {
1082                         RTE_LOG(ERR, VHOST_CONFIG,
1083                                 "failed to create fdset handling thread");
1084
1085                         fdset_pipe_uninit(&vhost_user.fdset);
1086                         return -1;
1087                 }
1088         }
1089
1090         if (vsocket->is_server)
1091                 return vhost_user_start_server(vsocket);
1092         else
1093                 return vhost_user_start_client(vsocket);
1094 }