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