eal: set name when creating a control thread
[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
471         ret = pthread_mutex_init(&reconn_list.mutex, NULL);
472         if (ret < 0) {
473                 RTE_LOG(ERR, VHOST_CONFIG, "failed to initialize mutex");
474                 return ret;
475         }
476         TAILQ_INIT(&reconn_list.head);
477
478         ret = rte_ctrl_thread_create(&reconn_tid, "vhost_reconn", NULL,
479                              vhost_user_client_reconnect, NULL);
480         if (ret != 0) {
481                 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
482                 if (pthread_mutex_destroy(&reconn_list.mutex)) {
483                         RTE_LOG(ERR, VHOST_CONFIG,
484                                 "failed to destroy reconnect mutex");
485                 }
486         }
487
488         return ret;
489 }
490
491 static int
492 vhost_user_start_client(struct vhost_user_socket *vsocket)
493 {
494         int ret;
495         int fd = vsocket->socket_fd;
496         const char *path = vsocket->path;
497         struct vhost_user_reconnect *reconn;
498
499         ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
500                                           sizeof(vsocket->un));
501         if (ret == 0) {
502                 vhost_user_add_connection(fd, vsocket);
503                 return 0;
504         }
505
506         RTE_LOG(WARNING, VHOST_CONFIG,
507                 "failed to connect to %s: %s\n",
508                 path, strerror(errno));
509
510         if (ret == -2 || !vsocket->reconnect) {
511                 close(fd);
512                 return -1;
513         }
514
515         RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
516         reconn = malloc(sizeof(*reconn));
517         if (reconn == NULL) {
518                 RTE_LOG(ERR, VHOST_CONFIG,
519                         "failed to allocate memory for reconnect\n");
520                 close(fd);
521                 return -1;
522         }
523         reconn->un = vsocket->un;
524         reconn->fd = fd;
525         reconn->vsocket = vsocket;
526         pthread_mutex_lock(&reconn_list.mutex);
527         TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
528         pthread_mutex_unlock(&reconn_list.mutex);
529
530         return 0;
531 }
532
533 static struct vhost_user_socket *
534 find_vhost_user_socket(const char *path)
535 {
536         int i;
537
538         for (i = 0; i < vhost_user.vsocket_cnt; i++) {
539                 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
540
541                 if (!strcmp(vsocket->path, path))
542                         return vsocket;
543         }
544
545         return NULL;
546 }
547
548 int
549 rte_vhost_driver_attach_vdpa_device(const char *path, int did)
550 {
551         struct vhost_user_socket *vsocket;
552
553         if (rte_vdpa_get_device(did) == NULL)
554                 return -1;
555
556         pthread_mutex_lock(&vhost_user.mutex);
557         vsocket = find_vhost_user_socket(path);
558         if (vsocket)
559                 vsocket->vdpa_dev_id = did;
560         pthread_mutex_unlock(&vhost_user.mutex);
561
562         return vsocket ? 0 : -1;
563 }
564
565 int
566 rte_vhost_driver_detach_vdpa_device(const char *path)
567 {
568         struct vhost_user_socket *vsocket;
569
570         pthread_mutex_lock(&vhost_user.mutex);
571         vsocket = find_vhost_user_socket(path);
572         if (vsocket)
573                 vsocket->vdpa_dev_id = -1;
574         pthread_mutex_unlock(&vhost_user.mutex);
575
576         return vsocket ? 0 : -1;
577 }
578
579 int
580 rte_vhost_driver_get_vdpa_device_id(const char *path)
581 {
582         struct vhost_user_socket *vsocket;
583         int did = -1;
584
585         pthread_mutex_lock(&vhost_user.mutex);
586         vsocket = find_vhost_user_socket(path);
587         if (vsocket)
588                 did = vsocket->vdpa_dev_id;
589         pthread_mutex_unlock(&vhost_user.mutex);
590
591         return did;
592 }
593
594 int
595 rte_vhost_driver_disable_features(const char *path, uint64_t features)
596 {
597         struct vhost_user_socket *vsocket;
598
599         pthread_mutex_lock(&vhost_user.mutex);
600         vsocket = find_vhost_user_socket(path);
601
602         /* Note that use_builtin_virtio_net is not affected by this function
603          * since callers may want to selectively disable features of the
604          * built-in vhost net device backend.
605          */
606
607         if (vsocket)
608                 vsocket->features &= ~features;
609         pthread_mutex_unlock(&vhost_user.mutex);
610
611         return vsocket ? 0 : -1;
612 }
613
614 int
615 rte_vhost_driver_enable_features(const char *path, uint64_t features)
616 {
617         struct vhost_user_socket *vsocket;
618
619         pthread_mutex_lock(&vhost_user.mutex);
620         vsocket = find_vhost_user_socket(path);
621         if (vsocket) {
622                 if ((vsocket->supported_features & features) != features) {
623                         /*
624                          * trying to enable features the driver doesn't
625                          * support.
626                          */
627                         pthread_mutex_unlock(&vhost_user.mutex);
628                         return -1;
629                 }
630                 vsocket->features |= features;
631         }
632         pthread_mutex_unlock(&vhost_user.mutex);
633
634         return vsocket ? 0 : -1;
635 }
636
637 int
638 rte_vhost_driver_set_features(const char *path, uint64_t features)
639 {
640         struct vhost_user_socket *vsocket;
641
642         pthread_mutex_lock(&vhost_user.mutex);
643         vsocket = find_vhost_user_socket(path);
644         if (vsocket) {
645                 vsocket->supported_features = features;
646                 vsocket->features = features;
647
648                 /* Anyone setting feature bits is implementing their own vhost
649                  * device backend.
650                  */
651                 vsocket->use_builtin_virtio_net = false;
652         }
653         pthread_mutex_unlock(&vhost_user.mutex);
654
655         return vsocket ? 0 : -1;
656 }
657
658 int
659 rte_vhost_driver_get_features(const char *path, uint64_t *features)
660 {
661         struct vhost_user_socket *vsocket;
662         uint64_t vdpa_features;
663         struct rte_vdpa_device *vdpa_dev;
664         int did = -1;
665         int ret = 0;
666
667         pthread_mutex_lock(&vhost_user.mutex);
668         vsocket = find_vhost_user_socket(path);
669         if (!vsocket) {
670                 RTE_LOG(ERR, VHOST_CONFIG,
671                         "socket file %s is not registered yet.\n", path);
672                 ret = -1;
673                 goto unlock_exit;
674         }
675
676         did = vsocket->vdpa_dev_id;
677         vdpa_dev = rte_vdpa_get_device(did);
678         if (!vdpa_dev || !vdpa_dev->ops->get_features) {
679                 *features = vsocket->features;
680                 goto unlock_exit;
681         }
682
683         if (vdpa_dev->ops->get_features(did, &vdpa_features) < 0) {
684                 RTE_LOG(ERR, VHOST_CONFIG,
685                                 "failed to get vdpa features "
686                                 "for socket file %s.\n", path);
687                 ret = -1;
688                 goto unlock_exit;
689         }
690
691         *features = vsocket->features & vdpa_features;
692
693 unlock_exit:
694         pthread_mutex_unlock(&vhost_user.mutex);
695         return ret;
696 }
697
698 int
699 rte_vhost_driver_get_protocol_features(const char *path,
700                 uint64_t *protocol_features)
701 {
702         struct vhost_user_socket *vsocket;
703         uint64_t vdpa_protocol_features;
704         struct rte_vdpa_device *vdpa_dev;
705         int did = -1;
706         int ret = 0;
707
708         pthread_mutex_lock(&vhost_user.mutex);
709         vsocket = find_vhost_user_socket(path);
710         if (!vsocket) {
711                 RTE_LOG(ERR, VHOST_CONFIG,
712                         "socket file %s is not registered yet.\n", path);
713                 ret = -1;
714                 goto unlock_exit;
715         }
716
717         did = vsocket->vdpa_dev_id;
718         vdpa_dev = rte_vdpa_get_device(did);
719         if (!vdpa_dev || !vdpa_dev->ops->get_protocol_features) {
720                 *protocol_features = VHOST_USER_PROTOCOL_FEATURES;
721                 goto unlock_exit;
722         }
723
724         if (vdpa_dev->ops->get_protocol_features(did,
725                                 &vdpa_protocol_features) < 0) {
726                 RTE_LOG(ERR, VHOST_CONFIG,
727                                 "failed to get vdpa protocol features "
728                                 "for socket file %s.\n", path);
729                 ret = -1;
730                 goto unlock_exit;
731         }
732
733         *protocol_features = VHOST_USER_PROTOCOL_FEATURES
734                 & vdpa_protocol_features;
735
736 unlock_exit:
737         pthread_mutex_unlock(&vhost_user.mutex);
738         return ret;
739 }
740
741 int
742 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num)
743 {
744         struct vhost_user_socket *vsocket;
745         uint32_t vdpa_queue_num;
746         struct rte_vdpa_device *vdpa_dev;
747         int did = -1;
748         int ret = 0;
749
750         pthread_mutex_lock(&vhost_user.mutex);
751         vsocket = find_vhost_user_socket(path);
752         if (!vsocket) {
753                 RTE_LOG(ERR, VHOST_CONFIG,
754                         "socket file %s is not registered yet.\n", path);
755                 ret = -1;
756                 goto unlock_exit;
757         }
758
759         did = vsocket->vdpa_dev_id;
760         vdpa_dev = rte_vdpa_get_device(did);
761         if (!vdpa_dev || !vdpa_dev->ops->get_queue_num) {
762                 *queue_num = VHOST_MAX_QUEUE_PAIRS;
763                 goto unlock_exit;
764         }
765
766         if (vdpa_dev->ops->get_queue_num(did, &vdpa_queue_num) < 0) {
767                 RTE_LOG(ERR, VHOST_CONFIG,
768                                 "failed to get vdpa queue number "
769                                 "for socket file %s.\n", path);
770                 ret = -1;
771                 goto unlock_exit;
772         }
773
774         *queue_num = RTE_MIN((uint32_t)VHOST_MAX_QUEUE_PAIRS, vdpa_queue_num);
775
776 unlock_exit:
777         pthread_mutex_unlock(&vhost_user.mutex);
778         return ret;
779 }
780
781 /*
782  * Register a new vhost-user socket; here we could act as server
783  * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
784  * is set.
785  */
786 int
787 rte_vhost_driver_register(const char *path, uint64_t flags)
788 {
789         int ret = -1;
790         struct vhost_user_socket *vsocket;
791
792         if (!path)
793                 return -1;
794
795         pthread_mutex_lock(&vhost_user.mutex);
796
797         if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
798                 RTE_LOG(ERR, VHOST_CONFIG,
799                         "error: the number of vhost sockets reaches maximum\n");
800                 goto out;
801         }
802
803         vsocket = malloc(sizeof(struct vhost_user_socket));
804         if (!vsocket)
805                 goto out;
806         memset(vsocket, 0, sizeof(struct vhost_user_socket));
807         vsocket->path = strdup(path);
808         if (vsocket->path == NULL) {
809                 RTE_LOG(ERR, VHOST_CONFIG,
810                         "error: failed to copy socket path string\n");
811                 free(vsocket);
812                 goto out;
813         }
814         TAILQ_INIT(&vsocket->conn_list);
815         ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
816         if (ret) {
817                 RTE_LOG(ERR, VHOST_CONFIG,
818                         "error: failed to init connection mutex\n");
819                 goto out_free;
820         }
821         vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
822
823         /*
824          * Set the supported features correctly for the builtin vhost-user
825          * net driver.
826          *
827          * Applications know nothing about features the builtin virtio net
828          * driver (virtio_net.c) supports, thus it's not possible for them
829          * to invoke rte_vhost_driver_set_features(). To workaround it, here
830          * we set it unconditionally. If the application want to implement
831          * another vhost-user driver (say SCSI), it should call the
832          * rte_vhost_driver_set_features(), which will overwrite following
833          * two values.
834          */
835         vsocket->use_builtin_virtio_net = true;
836         vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
837         vsocket->features           = VIRTIO_NET_SUPPORTED_FEATURES;
838
839         if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
840                 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
841                 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
842         }
843
844         if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
845                 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
846                 if (vsocket->reconnect && reconn_tid == 0) {
847                         if (vhost_user_reconnect_init() != 0)
848                                 goto out_mutex;
849                 }
850         } else {
851                 vsocket->is_server = true;
852         }
853         ret = create_unix_socket(vsocket);
854         if (ret < 0) {
855                 goto out_mutex;
856         }
857
858         vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
859
860         pthread_mutex_unlock(&vhost_user.mutex);
861         return ret;
862
863 out_mutex:
864         if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
865                 RTE_LOG(ERR, VHOST_CONFIG,
866                         "error: failed to destroy connection mutex\n");
867         }
868 out_free:
869         free(vsocket->path);
870         free(vsocket);
871 out:
872         pthread_mutex_unlock(&vhost_user.mutex);
873
874         return ret;
875 }
876
877 static bool
878 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
879 {
880         int found = false;
881         struct vhost_user_reconnect *reconn, *next;
882
883         pthread_mutex_lock(&reconn_list.mutex);
884
885         for (reconn = TAILQ_FIRST(&reconn_list.head);
886              reconn != NULL; reconn = next) {
887                 next = TAILQ_NEXT(reconn, next);
888
889                 if (reconn->vsocket == vsocket) {
890                         TAILQ_REMOVE(&reconn_list.head, reconn, next);
891                         close(reconn->fd);
892                         free(reconn);
893                         found = true;
894                         break;
895                 }
896         }
897         pthread_mutex_unlock(&reconn_list.mutex);
898         return found;
899 }
900
901 /**
902  * Unregister the specified vhost socket
903  */
904 int
905 rte_vhost_driver_unregister(const char *path)
906 {
907         int i;
908         int count;
909         struct vhost_user_connection *conn, *next;
910
911         pthread_mutex_lock(&vhost_user.mutex);
912
913         for (i = 0; i < vhost_user.vsocket_cnt; i++) {
914                 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
915
916                 if (!strcmp(vsocket->path, path)) {
917                         if (vsocket->is_server) {
918                                 fdset_del(&vhost_user.fdset, vsocket->socket_fd);
919                                 close(vsocket->socket_fd);
920                                 unlink(path);
921                         } else if (vsocket->reconnect) {
922                                 vhost_user_remove_reconnect(vsocket);
923                         }
924
925                         pthread_mutex_lock(&vsocket->conn_mutex);
926                         for (conn = TAILQ_FIRST(&vsocket->conn_list);
927                              conn != NULL;
928                              conn = next) {
929                                 next = TAILQ_NEXT(conn, next);
930
931                                 fdset_del(&vhost_user.fdset, conn->connfd);
932                                 RTE_LOG(INFO, VHOST_CONFIG,
933                                         "free connfd = %d for device '%s'\n",
934                                         conn->connfd, path);
935                                 close(conn->connfd);
936                                 vhost_destroy_device(conn->vid);
937                                 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
938                                 free(conn);
939                         }
940                         pthread_mutex_unlock(&vsocket->conn_mutex);
941
942                         pthread_mutex_destroy(&vsocket->conn_mutex);
943                         free(vsocket->path);
944                         free(vsocket);
945
946                         count = --vhost_user.vsocket_cnt;
947                         vhost_user.vsockets[i] = vhost_user.vsockets[count];
948                         vhost_user.vsockets[count] = NULL;
949                         pthread_mutex_unlock(&vhost_user.mutex);
950
951                         return 0;
952                 }
953         }
954         pthread_mutex_unlock(&vhost_user.mutex);
955
956         return -1;
957 }
958
959 /*
960  * Register ops so that we can add/remove device to data core.
961  */
962 int
963 rte_vhost_driver_callback_register(const char *path,
964         struct vhost_device_ops const * const ops)
965 {
966         struct vhost_user_socket *vsocket;
967
968         pthread_mutex_lock(&vhost_user.mutex);
969         vsocket = find_vhost_user_socket(path);
970         if (vsocket)
971                 vsocket->notify_ops = ops;
972         pthread_mutex_unlock(&vhost_user.mutex);
973
974         return vsocket ? 0 : -1;
975 }
976
977 struct vhost_device_ops const *
978 vhost_driver_callback_get(const char *path)
979 {
980         struct vhost_user_socket *vsocket;
981
982         pthread_mutex_lock(&vhost_user.mutex);
983         vsocket = find_vhost_user_socket(path);
984         pthread_mutex_unlock(&vhost_user.mutex);
985
986         return vsocket ? vsocket->notify_ops : NULL;
987 }
988
989 int
990 rte_vhost_driver_start(const char *path)
991 {
992         struct vhost_user_socket *vsocket;
993         static pthread_t fdset_tid;
994
995         pthread_mutex_lock(&vhost_user.mutex);
996         vsocket = find_vhost_user_socket(path);
997         pthread_mutex_unlock(&vhost_user.mutex);
998
999         if (!vsocket)
1000                 return -1;
1001
1002         if (fdset_tid == 0) {
1003                 /**
1004                  * create a pipe which will be waited by poll and notified to
1005                  * rebuild the wait list of poll.
1006                  */
1007                 if (fdset_pipe_init(&vhost_user.fdset) < 0) {
1008                         RTE_LOG(ERR, VHOST_CONFIG,
1009                                 "failed to create pipe for vhost fdset\n");
1010                         return -1;
1011                 }
1012
1013                 int ret = rte_ctrl_thread_create(&fdset_tid,
1014                         "vhost-events", NULL, fdset_event_dispatch,
1015                         &vhost_user.fdset);
1016                 if (ret != 0) {
1017                         RTE_LOG(ERR, VHOST_CONFIG,
1018                                 "failed to create fdset handling thread");
1019
1020                         fdset_pipe_uninit(&vhost_user.fdset);
1021                         return -1;
1022                 }
1023         }
1024
1025         if (vsocket->is_server)
1026                 return vhost_user_start_server(vsocket);
1027         else
1028                 return vhost_user_start_client(vsocket);
1029 }