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