vhost: add number of fds to vhost-user messages
[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 /*
98  * return bytes# of read on success or negative val on failure. Update fdnum
99  * with number of fds read.
100  */
101 int
102 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int max_fds,
103                 int *fd_num)
104 {
105         struct iovec iov;
106         struct msghdr msgh;
107         char control[CMSG_SPACE(max_fds * sizeof(int))];
108         struct cmsghdr *cmsg;
109         int got_fds = 0;
110         int ret;
111
112         *fd_num = 0;
113
114         memset(&msgh, 0, sizeof(msgh));
115         iov.iov_base = buf;
116         iov.iov_len  = buflen;
117
118         msgh.msg_iov = &iov;
119         msgh.msg_iovlen = 1;
120         msgh.msg_control = control;
121         msgh.msg_controllen = sizeof(control);
122
123         ret = recvmsg(sockfd, &msgh, 0);
124         if (ret <= 0) {
125                 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
126                 return ret;
127         }
128
129         if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
130                 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
131                 return -1;
132         }
133
134         for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
135                 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
136                 if ((cmsg->cmsg_level == SOL_SOCKET) &&
137                         (cmsg->cmsg_type == SCM_RIGHTS)) {
138                         got_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
139                         *fd_num = got_fds;
140                         memcpy(fds, CMSG_DATA(cmsg), got_fds * sizeof(int));
141                         break;
142                 }
143         }
144
145         /* Clear out unused file descriptors */
146         while (got_fds < max_fds)
147                 fds[got_fds++] = -1;
148
149         return ret;
150 }
151
152 int
153 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
154 {
155
156         struct iovec iov;
157         struct msghdr msgh;
158         size_t fdsize = fd_num * sizeof(int);
159         char control[CMSG_SPACE(fdsize)];
160         struct cmsghdr *cmsg;
161         int ret;
162
163         memset(&msgh, 0, sizeof(msgh));
164         iov.iov_base = buf;
165         iov.iov_len = buflen;
166
167         msgh.msg_iov = &iov;
168         msgh.msg_iovlen = 1;
169
170         if (fds && fd_num > 0) {
171                 msgh.msg_control = control;
172                 msgh.msg_controllen = sizeof(control);
173                 cmsg = CMSG_FIRSTHDR(&msgh);
174                 if (cmsg == NULL) {
175                         RTE_LOG(ERR, VHOST_CONFIG, "cmsg == NULL\n");
176                         errno = EINVAL;
177                         return -1;
178                 }
179                 cmsg->cmsg_len = CMSG_LEN(fdsize);
180                 cmsg->cmsg_level = SOL_SOCKET;
181                 cmsg->cmsg_type = SCM_RIGHTS;
182                 memcpy(CMSG_DATA(cmsg), fds, fdsize);
183         } else {
184                 msgh.msg_control = NULL;
185                 msgh.msg_controllen = 0;
186         }
187
188         do {
189                 ret = sendmsg(sockfd, &msgh, MSG_NOSIGNAL);
190         } while (ret < 0 && errno == EINTR);
191
192         if (ret < 0) {
193                 RTE_LOG(ERR, VHOST_CONFIG,  "sendmsg error\n");
194                 return ret;
195         }
196
197         return ret;
198 }
199
200 static void
201 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
202 {
203         int vid;
204         size_t size;
205         struct vhost_user_connection *conn;
206         int ret;
207
208         if (vsocket == NULL)
209                 return;
210
211         conn = malloc(sizeof(*conn));
212         if (conn == NULL) {
213                 close(fd);
214                 return;
215         }
216
217         vid = vhost_new_device();
218         if (vid == -1) {
219                 goto err;
220         }
221
222         size = strnlen(vsocket->path, PATH_MAX);
223         vhost_set_ifname(vid, vsocket->path, size);
224
225         vhost_set_builtin_virtio_net(vid, vsocket->use_builtin_virtio_net);
226
227         vhost_attach_vdpa_device(vid, vsocket->vdpa_dev_id);
228
229         if (vsocket->dequeue_zero_copy)
230                 vhost_enable_dequeue_zero_copy(vid);
231
232         RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
233
234         if (vsocket->notify_ops->new_connection) {
235                 ret = vsocket->notify_ops->new_connection(vid);
236                 if (ret < 0) {
237                         RTE_LOG(ERR, VHOST_CONFIG,
238                                 "failed to add vhost user connection with fd %d\n",
239                                 fd);
240                         goto err;
241                 }
242         }
243
244         conn->connfd = fd;
245         conn->vsocket = vsocket;
246         conn->vid = vid;
247         ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
248                         NULL, conn);
249         if (ret < 0) {
250                 RTE_LOG(ERR, VHOST_CONFIG,
251                         "failed to add fd %d into vhost server fdset\n",
252                         fd);
253
254                 if (vsocket->notify_ops->destroy_connection)
255                         vsocket->notify_ops->destroy_connection(conn->vid);
256
257                 goto err;
258         }
259
260         pthread_mutex_lock(&vsocket->conn_mutex);
261         TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
262         pthread_mutex_unlock(&vsocket->conn_mutex);
263
264         fdset_pipe_notify(&vhost_user.fdset);
265         return;
266
267 err:
268         free(conn);
269         close(fd);
270 }
271
272 /* call back when there is new vhost-user connection from client  */
273 static void
274 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
275 {
276         struct vhost_user_socket *vsocket = dat;
277
278         fd = accept(fd, NULL, NULL);
279         if (fd < 0)
280                 return;
281
282         RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
283         vhost_user_add_connection(fd, vsocket);
284 }
285
286 static void
287 vhost_user_read_cb(int connfd, void *dat, int *remove)
288 {
289         struct vhost_user_connection *conn = dat;
290         struct vhost_user_socket *vsocket = conn->vsocket;
291         int ret;
292
293         ret = vhost_user_msg_handler(conn->vid, connfd);
294         if (ret < 0) {
295                 close(connfd);
296                 *remove = 1;
297                 vhost_destroy_device(conn->vid);
298
299                 if (vsocket->notify_ops->destroy_connection)
300                         vsocket->notify_ops->destroy_connection(conn->vid);
301
302                 pthread_mutex_lock(&vsocket->conn_mutex);
303                 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
304                 pthread_mutex_unlock(&vsocket->conn_mutex);
305
306                 free(conn);
307
308                 if (vsocket->reconnect) {
309                         create_unix_socket(vsocket);
310                         vhost_user_start_client(vsocket);
311                 }
312         }
313 }
314
315 static int
316 create_unix_socket(struct vhost_user_socket *vsocket)
317 {
318         int fd;
319         struct sockaddr_un *un = &vsocket->un;
320
321         fd = socket(AF_UNIX, SOCK_STREAM, 0);
322         if (fd < 0)
323                 return -1;
324         RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
325                 vsocket->is_server ? "server" : "client", fd);
326
327         if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
328                 RTE_LOG(ERR, VHOST_CONFIG,
329                         "vhost-user: can't set nonblocking mode for socket, fd: "
330                         "%d (%s)\n", fd, strerror(errno));
331                 close(fd);
332                 return -1;
333         }
334
335         memset(un, 0, sizeof(*un));
336         un->sun_family = AF_UNIX;
337         strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
338         un->sun_path[sizeof(un->sun_path) - 1] = '\0';
339
340         vsocket->socket_fd = fd;
341         return 0;
342 }
343
344 static int
345 vhost_user_start_server(struct vhost_user_socket *vsocket)
346 {
347         int ret;
348         int fd = vsocket->socket_fd;
349         const char *path = vsocket->path;
350
351         /*
352          * bind () may fail if the socket file with the same name already
353          * exists. But the library obviously should not delete the file
354          * provided by the user, since we can not be sure that it is not
355          * being used by other applications. Moreover, many applications form
356          * socket names based on user input, which is prone to errors.
357          *
358          * The user must ensure that the socket does not exist before
359          * registering the vhost driver in server mode.
360          */
361         ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
362         if (ret < 0) {
363                 RTE_LOG(ERR, VHOST_CONFIG,
364                         "failed to bind to %s: %s; remove it and try again\n",
365                         path, strerror(errno));
366                 goto err;
367         }
368         RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
369
370         ret = listen(fd, MAX_VIRTIO_BACKLOG);
371         if (ret < 0)
372                 goto err;
373
374         ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
375                   NULL, vsocket);
376         if (ret < 0) {
377                 RTE_LOG(ERR, VHOST_CONFIG,
378                         "failed to add listen fd %d to vhost server fdset\n",
379                         fd);
380                 goto err;
381         }
382
383         return 0;
384
385 err:
386         close(fd);
387         return -1;
388 }
389
390 struct vhost_user_reconnect {
391         struct sockaddr_un un;
392         int fd;
393         struct vhost_user_socket *vsocket;
394
395         TAILQ_ENTRY(vhost_user_reconnect) next;
396 };
397
398 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
399 struct vhost_user_reconnect_list {
400         struct vhost_user_reconnect_tailq_list head;
401         pthread_mutex_t mutex;
402 };
403
404 static struct vhost_user_reconnect_list reconn_list;
405 static pthread_t reconn_tid;
406
407 static int
408 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
409 {
410         int ret, flags;
411
412         ret = connect(fd, un, sz);
413         if (ret < 0 && errno != EISCONN)
414                 return -1;
415
416         flags = fcntl(fd, F_GETFL, 0);
417         if (flags < 0) {
418                 RTE_LOG(ERR, VHOST_CONFIG,
419                         "can't get flags for connfd %d\n", fd);
420                 return -2;
421         }
422         if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
423                 RTE_LOG(ERR, VHOST_CONFIG,
424                                 "can't disable nonblocking on fd %d\n", fd);
425                 return -2;
426         }
427         return 0;
428 }
429
430 static void *
431 vhost_user_client_reconnect(void *arg __rte_unused)
432 {
433         int ret;
434         struct vhost_user_reconnect *reconn, *next;
435
436         while (1) {
437                 pthread_mutex_lock(&reconn_list.mutex);
438
439                 /*
440                  * An equal implementation of TAILQ_FOREACH_SAFE,
441                  * which does not exist on all platforms.
442                  */
443                 for (reconn = TAILQ_FIRST(&reconn_list.head);
444                      reconn != NULL; reconn = next) {
445                         next = TAILQ_NEXT(reconn, next);
446
447                         ret = vhost_user_connect_nonblock(reconn->fd,
448                                                 (struct sockaddr *)&reconn->un,
449                                                 sizeof(reconn->un));
450                         if (ret == -2) {
451                                 close(reconn->fd);
452                                 RTE_LOG(ERR, VHOST_CONFIG,
453                                         "reconnection for fd %d failed\n",
454                                         reconn->fd);
455                                 goto remove_fd;
456                         }
457                         if (ret == -1)
458                                 continue;
459
460                         RTE_LOG(INFO, VHOST_CONFIG,
461                                 "%s: connected\n", reconn->vsocket->path);
462                         vhost_user_add_connection(reconn->fd, reconn->vsocket);
463 remove_fd:
464                         TAILQ_REMOVE(&reconn_list.head, reconn, next);
465                         free(reconn);
466                 }
467
468                 pthread_mutex_unlock(&reconn_list.mutex);
469                 sleep(1);
470         }
471
472         return NULL;
473 }
474
475 static int
476 vhost_user_reconnect_init(void)
477 {
478         int ret;
479
480         ret = pthread_mutex_init(&reconn_list.mutex, NULL);
481         if (ret < 0) {
482                 RTE_LOG(ERR, VHOST_CONFIG, "failed to initialize mutex");
483                 return ret;
484         }
485         TAILQ_INIT(&reconn_list.head);
486
487         ret = rte_ctrl_thread_create(&reconn_tid, "vhost_reconn", NULL,
488                              vhost_user_client_reconnect, NULL);
489         if (ret != 0) {
490                 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
491                 if (pthread_mutex_destroy(&reconn_list.mutex)) {
492                         RTE_LOG(ERR, VHOST_CONFIG,
493                                 "failed to destroy reconnect mutex");
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 static void
791 vhost_user_socket_mem_free(struct vhost_user_socket *vsocket)
792 {
793         if (vsocket && vsocket->path) {
794                 free(vsocket->path);
795                 vsocket->path = NULL;
796         }
797
798         if (vsocket) {
799                 free(vsocket);
800                 vsocket = NULL;
801         }
802 }
803
804 /*
805  * Register a new vhost-user socket; here we could act as server
806  * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
807  * is set.
808  */
809 int
810 rte_vhost_driver_register(const char *path, uint64_t flags)
811 {
812         int ret = -1;
813         struct vhost_user_socket *vsocket;
814
815         if (!path)
816                 return -1;
817
818         pthread_mutex_lock(&vhost_user.mutex);
819
820         if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
821                 RTE_LOG(ERR, VHOST_CONFIG,
822                         "error: the number of vhost sockets reaches maximum\n");
823                 goto out;
824         }
825
826         vsocket = malloc(sizeof(struct vhost_user_socket));
827         if (!vsocket)
828                 goto out;
829         memset(vsocket, 0, sizeof(struct vhost_user_socket));
830         vsocket->path = strdup(path);
831         if (vsocket->path == NULL) {
832                 RTE_LOG(ERR, VHOST_CONFIG,
833                         "error: failed to copy socket path string\n");
834                 vhost_user_socket_mem_free(vsocket);
835                 goto out;
836         }
837         TAILQ_INIT(&vsocket->conn_list);
838         ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
839         if (ret) {
840                 RTE_LOG(ERR, VHOST_CONFIG,
841                         "error: failed to init connection mutex\n");
842                 goto out_free;
843         }
844         vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
845
846         /*
847          * Set the supported features correctly for the builtin vhost-user
848          * net driver.
849          *
850          * Applications know nothing about features the builtin virtio net
851          * driver (virtio_net.c) supports, thus it's not possible for them
852          * to invoke rte_vhost_driver_set_features(). To workaround it, here
853          * we set it unconditionally. If the application want to implement
854          * another vhost-user driver (say SCSI), it should call the
855          * rte_vhost_driver_set_features(), which will overwrite following
856          * two values.
857          */
858         vsocket->use_builtin_virtio_net = true;
859         vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
860         vsocket->features           = VIRTIO_NET_SUPPORTED_FEATURES;
861
862         /* Dequeue zero copy can't assure descriptors returned in order */
863         if (vsocket->dequeue_zero_copy) {
864                 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IN_ORDER);
865                 vsocket->features &= ~(1ULL << VIRTIO_F_IN_ORDER);
866         }
867
868         if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
869                 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
870                 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
871         }
872
873         if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
874                 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
875                 if (vsocket->reconnect && reconn_tid == 0) {
876                         if (vhost_user_reconnect_init() != 0)
877                                 goto out_mutex;
878                 }
879         } else {
880                 vsocket->is_server = true;
881         }
882         ret = create_unix_socket(vsocket);
883         if (ret < 0) {
884                 goto out_mutex;
885         }
886
887         vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
888
889         pthread_mutex_unlock(&vhost_user.mutex);
890         return ret;
891
892 out_mutex:
893         if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
894                 RTE_LOG(ERR, VHOST_CONFIG,
895                         "error: failed to destroy connection mutex\n");
896         }
897 out_free:
898         vhost_user_socket_mem_free(vsocket);
899 out:
900         pthread_mutex_unlock(&vhost_user.mutex);
901
902         return ret;
903 }
904
905 static bool
906 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
907 {
908         int found = false;
909         struct vhost_user_reconnect *reconn, *next;
910
911         pthread_mutex_lock(&reconn_list.mutex);
912
913         for (reconn = TAILQ_FIRST(&reconn_list.head);
914              reconn != NULL; reconn = next) {
915                 next = TAILQ_NEXT(reconn, next);
916
917                 if (reconn->vsocket == vsocket) {
918                         TAILQ_REMOVE(&reconn_list.head, reconn, next);
919                         close(reconn->fd);
920                         free(reconn);
921                         found = true;
922                         break;
923                 }
924         }
925         pthread_mutex_unlock(&reconn_list.mutex);
926         return found;
927 }
928
929 /**
930  * Unregister the specified vhost socket
931  */
932 int
933 rte_vhost_driver_unregister(const char *path)
934 {
935         int i;
936         int count;
937         struct vhost_user_connection *conn, *next;
938
939         pthread_mutex_lock(&vhost_user.mutex);
940
941         for (i = 0; i < vhost_user.vsocket_cnt; i++) {
942                 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
943
944                 if (!strcmp(vsocket->path, path)) {
945 again:
946                         pthread_mutex_lock(&vsocket->conn_mutex);
947                         for (conn = TAILQ_FIRST(&vsocket->conn_list);
948                              conn != NULL;
949                              conn = next) {
950                                 next = TAILQ_NEXT(conn, next);
951
952                                 /*
953                                  * If r/wcb is executing, release the
954                                  * conn_mutex lock, and try again since
955                                  * the r/wcb may use the conn_mutex lock.
956                                  */
957                                 if (fdset_try_del(&vhost_user.fdset,
958                                                   conn->connfd) == -1) {
959                                         pthread_mutex_unlock(
960                                                         &vsocket->conn_mutex);
961                                         goto again;
962                                 }
963
964                                 RTE_LOG(INFO, VHOST_CONFIG,
965                                         "free connfd = %d for device '%s'\n",
966                                         conn->connfd, path);
967                                 close(conn->connfd);
968                                 vhost_destroy_device(conn->vid);
969                                 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
970                                 free(conn);
971                         }
972                         pthread_mutex_unlock(&vsocket->conn_mutex);
973
974                         if (vsocket->is_server) {
975                                 fdset_del(&vhost_user.fdset,
976                                                 vsocket->socket_fd);
977                                 close(vsocket->socket_fd);
978                                 unlink(path);
979                         } else if (vsocket->reconnect) {
980                                 vhost_user_remove_reconnect(vsocket);
981                         }
982
983                         pthread_mutex_destroy(&vsocket->conn_mutex);
984                         vhost_user_socket_mem_free(vsocket);
985
986                         count = --vhost_user.vsocket_cnt;
987                         vhost_user.vsockets[i] = vhost_user.vsockets[count];
988                         vhost_user.vsockets[count] = NULL;
989                         pthread_mutex_unlock(&vhost_user.mutex);
990
991                         return 0;
992                 }
993         }
994         pthread_mutex_unlock(&vhost_user.mutex);
995
996         return -1;
997 }
998
999 /*
1000  * Register ops so that we can add/remove device to data core.
1001  */
1002 int
1003 rte_vhost_driver_callback_register(const char *path,
1004         struct vhost_device_ops const * const ops)
1005 {
1006         struct vhost_user_socket *vsocket;
1007
1008         pthread_mutex_lock(&vhost_user.mutex);
1009         vsocket = find_vhost_user_socket(path);
1010         if (vsocket)
1011                 vsocket->notify_ops = ops;
1012         pthread_mutex_unlock(&vhost_user.mutex);
1013
1014         return vsocket ? 0 : -1;
1015 }
1016
1017 struct vhost_device_ops const *
1018 vhost_driver_callback_get(const char *path)
1019 {
1020         struct vhost_user_socket *vsocket;
1021
1022         pthread_mutex_lock(&vhost_user.mutex);
1023         vsocket = find_vhost_user_socket(path);
1024         pthread_mutex_unlock(&vhost_user.mutex);
1025
1026         return vsocket ? vsocket->notify_ops : NULL;
1027 }
1028
1029 int
1030 rte_vhost_driver_start(const char *path)
1031 {
1032         struct vhost_user_socket *vsocket;
1033         static pthread_t fdset_tid;
1034
1035         pthread_mutex_lock(&vhost_user.mutex);
1036         vsocket = find_vhost_user_socket(path);
1037         pthread_mutex_unlock(&vhost_user.mutex);
1038
1039         if (!vsocket)
1040                 return -1;
1041
1042         if (fdset_tid == 0) {
1043                 /**
1044                  * create a pipe which will be waited by poll and notified to
1045                  * rebuild the wait list of poll.
1046                  */
1047                 if (fdset_pipe_init(&vhost_user.fdset) < 0) {
1048                         RTE_LOG(ERR, VHOST_CONFIG,
1049                                 "failed to create pipe for vhost fdset\n");
1050                         return -1;
1051                 }
1052
1053                 int ret = rte_ctrl_thread_create(&fdset_tid,
1054                         "vhost-events", NULL, fdset_event_dispatch,
1055                         &vhost_user.fdset);
1056                 if (ret != 0) {
1057                         RTE_LOG(ERR, VHOST_CONFIG,
1058                                 "failed to create fdset handling thread");
1059
1060                         fdset_pipe_uninit(&vhost_user.fdset);
1061                         return -1;
1062                 }
1063         }
1064
1065         if (vsocket->is_server)
1066                 return vhost_user_start_server(vsocket);
1067         else
1068                 return vhost_user_start_client(vsocket);
1069 }