85c64485c2bb0af42d7f00d1f782823ed2c5bd7a
[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                 VHOST_LOG_CONFIG(ERR, "recvmsg failed\n");
131                 return ret;
132         }
133
134         if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
135                 VHOST_LOG_CONFIG(ERR, "truncated 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                         VHOST_LOG_CONFIG(ERR, "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                 VHOST_LOG_CONFIG(ERR,  "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         VHOST_LOG_CONFIG(INFO, "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                         VHOST_LOG_CONFIG(ERR,
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                 VHOST_LOG_CONFIG(ERR,
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         VHOST_LOG_CONFIG(INFO, "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                 if (vsocket->reconnect) {
322                         create_unix_socket(vsocket);
323                         vhost_user_start_client(vsocket);
324                 }
325
326                 pthread_mutex_lock(&vsocket->conn_mutex);
327                 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
328                 pthread_mutex_unlock(&vsocket->conn_mutex);
329
330                 free(conn);
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         VHOST_LOG_CONFIG(INFO, "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                 VHOST_LOG_CONFIG(ERR,
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                 VHOST_LOG_CONFIG(ERR,
383                         "failed to bind to %s: %s; remove it and try again\n",
384                         path, strerror(errno));
385                 goto err;
386         }
387         VHOST_LOG_CONFIG(INFO, "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                 VHOST_LOG_CONFIG(ERR,
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                 VHOST_LOG_CONFIG(ERR,
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                 VHOST_LOG_CONFIG(ERR,
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                                 VHOST_LOG_CONFIG(ERR,
472                                         "reconnection for fd %d failed\n",
473                                         reconn->fd);
474                                 goto remove_fd;
475                         }
476                         if (ret == -1)
477                                 continue;
478
479                         VHOST_LOG_CONFIG(INFO,
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                 VHOST_LOG_CONFIG(ERR, "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                 VHOST_LOG_CONFIG(ERR, "failed to create reconnect thread");
510                 if (pthread_mutex_destroy(&reconn_list.mutex)) {
511                         VHOST_LOG_CONFIG(ERR,
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         VHOST_LOG_CONFIG(WARNING,
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         VHOST_LOG_CONFIG(INFO, "%s: reconnecting...\n", path);
544         reconn = malloc(sizeof(*reconn));
545         if (reconn == NULL) {
546                 VHOST_LOG_CONFIG(ERR,
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                 VHOST_LOG_CONFIG(ERR,
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                 VHOST_LOG_CONFIG(ERR,
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                 VHOST_LOG_CONFIG(ERR,
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                 VHOST_LOG_CONFIG(ERR,
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                 VHOST_LOG_CONFIG(ERR,
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                 VHOST_LOG_CONFIG(ERR,
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                 VHOST_LOG_CONFIG(ERR,
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                 VHOST_LOG_CONFIG(ERR,
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                 VHOST_LOG_CONFIG(ERR,
877                         "error: failed to init connection mutex\n");
878                 goto out_free;
879         }
880         vsocket->vdpa_dev_id = -1;
881         vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
882         vsocket->extbuf = flags & RTE_VHOST_USER_EXTBUF_SUPPORT;
883         vsocket->linearbuf = flags & RTE_VHOST_USER_LINEARBUF_SUPPORT;
884
885         if (vsocket->dequeue_zero_copy &&
886             (flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
887                 VHOST_LOG_CONFIG(ERR,
888                         "error: enabling dequeue zero copy and IOMMU features "
889                         "simultaneously is not supported\n");
890                 goto out_mutex;
891         }
892
893         /*
894          * Set the supported features correctly for the builtin vhost-user
895          * net driver.
896          *
897          * Applications know nothing about features the builtin virtio net
898          * driver (virtio_net.c) supports, thus it's not possible for them
899          * to invoke rte_vhost_driver_set_features(). To workaround it, here
900          * we set it unconditionally. If the application want to implement
901          * another vhost-user driver (say SCSI), it should call the
902          * rte_vhost_driver_set_features(), which will overwrite following
903          * two values.
904          */
905         vsocket->use_builtin_virtio_net = true;
906         vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
907         vsocket->features           = VIRTIO_NET_SUPPORTED_FEATURES;
908         vsocket->protocol_features  = VHOST_USER_PROTOCOL_FEATURES;
909
910         /*
911          * Dequeue zero copy can't assure descriptors returned in order.
912          * Also, it requires that the guest memory is populated, which is
913          * not compatible with postcopy.
914          */
915         if (vsocket->dequeue_zero_copy) {
916                 if (vsocket->extbuf) {
917                         VHOST_LOG_CONFIG(ERR,
918                         "error: zero copy is incompatible with external buffers\n");
919                         ret = -1;
920                         goto out_mutex;
921                 }
922                 if (vsocket->linearbuf) {
923                         VHOST_LOG_CONFIG(ERR,
924                         "error: zero copy is incompatible with linear buffers\n");
925                         ret = -1;
926                         goto out_mutex;
927                 }
928                 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IN_ORDER);
929                 vsocket->features &= ~(1ULL << VIRTIO_F_IN_ORDER);
930
931                 VHOST_LOG_CONFIG(INFO,
932                         "Dequeue zero copy requested, disabling postcopy support\n");
933                 vsocket->protocol_features &=
934                         ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
935         }
936
937         /*
938          * We'll not be able to receive a buffer from guest in linear mode
939          * without external buffer if it will not fit in a single mbuf, which is
940          * likely if segmentation offloading enabled.
941          */
942         if (vsocket->linearbuf && !vsocket->extbuf) {
943                 uint64_t seg_offload_features =
944                                 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
945                                 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
946                                 (1ULL << VIRTIO_NET_F_HOST_UFO);
947
948                 VHOST_LOG_CONFIG(INFO,
949                         "Linear buffers requested without external buffers, "
950                         "disabling host segmentation offloading support\n");
951                 vsocket->supported_features &= ~seg_offload_features;
952                 vsocket->features &= ~seg_offload_features;
953         }
954
955         if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
956                 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
957                 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
958         }
959
960         if (!(flags & RTE_VHOST_USER_POSTCOPY_SUPPORT)) {
961                 vsocket->protocol_features &=
962                         ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
963         } else {
964 #ifndef RTE_LIBRTE_VHOST_POSTCOPY
965                 VHOST_LOG_CONFIG(ERR,
966                         "Postcopy requested but not compiled\n");
967                 ret = -1;
968                 goto out_mutex;
969 #endif
970         }
971
972         if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
973                 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
974                 if (vsocket->reconnect && reconn_tid == 0) {
975                         if (vhost_user_reconnect_init() != 0)
976                                 goto out_mutex;
977                 }
978         } else {
979                 vsocket->is_server = true;
980         }
981         ret = create_unix_socket(vsocket);
982         if (ret < 0) {
983                 goto out_mutex;
984         }
985
986         vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
987
988         pthread_mutex_unlock(&vhost_user.mutex);
989         return ret;
990
991 out_mutex:
992         if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
993                 VHOST_LOG_CONFIG(ERR,
994                         "error: failed to destroy connection mutex\n");
995         }
996 out_free:
997         vhost_user_socket_mem_free(vsocket);
998 out:
999         pthread_mutex_unlock(&vhost_user.mutex);
1000
1001         return ret;
1002 }
1003
1004 static bool
1005 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
1006 {
1007         int found = false;
1008         struct vhost_user_reconnect *reconn, *next;
1009
1010         pthread_mutex_lock(&reconn_list.mutex);
1011
1012         for (reconn = TAILQ_FIRST(&reconn_list.head);
1013              reconn != NULL; reconn = next) {
1014                 next = TAILQ_NEXT(reconn, next);
1015
1016                 if (reconn->vsocket == vsocket) {
1017                         TAILQ_REMOVE(&reconn_list.head, reconn, next);
1018                         close(reconn->fd);
1019                         free(reconn);
1020                         found = true;
1021                         break;
1022                 }
1023         }
1024         pthread_mutex_unlock(&reconn_list.mutex);
1025         return found;
1026 }
1027
1028 /**
1029  * Unregister the specified vhost socket
1030  */
1031 int
1032 rte_vhost_driver_unregister(const char *path)
1033 {
1034         int i;
1035         int count;
1036         struct vhost_user_connection *conn, *next;
1037
1038         if (path == NULL)
1039                 return -1;
1040
1041 again:
1042         pthread_mutex_lock(&vhost_user.mutex);
1043
1044         for (i = 0; i < vhost_user.vsocket_cnt; i++) {
1045                 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
1046
1047                 if (!strcmp(vsocket->path, path)) {
1048                         pthread_mutex_lock(&vsocket->conn_mutex);
1049                         for (conn = TAILQ_FIRST(&vsocket->conn_list);
1050                              conn != NULL;
1051                              conn = next) {
1052                                 next = TAILQ_NEXT(conn, next);
1053
1054                                 /*
1055                                  * If r/wcb is executing, release vsocket's
1056                                  * conn_mutex and vhost_user's mutex locks, and
1057                                  * try again since the r/wcb may use the
1058                                  * conn_mutex and mutex locks.
1059                                  */
1060                                 if (fdset_try_del(&vhost_user.fdset,
1061                                                   conn->connfd) == -1) {
1062                                         pthread_mutex_unlock(
1063                                                         &vsocket->conn_mutex);
1064                                         pthread_mutex_unlock(&vhost_user.mutex);
1065                                         goto again;
1066                                 }
1067
1068                                 VHOST_LOG_CONFIG(INFO,
1069                                         "free connfd = %d for device '%s'\n",
1070                                         conn->connfd, path);
1071                                 close(conn->connfd);
1072                                 vhost_destroy_device(conn->vid);
1073                                 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
1074                                 free(conn);
1075                         }
1076                         pthread_mutex_unlock(&vsocket->conn_mutex);
1077
1078                         if (vsocket->is_server) {
1079                                 /*
1080                                  * If r/wcb is executing, release vhost_user's
1081                                  * mutex lock, and try again since the r/wcb
1082                                  * may use the mutex lock.
1083                                  */
1084                                 if (fdset_try_del(&vhost_user.fdset,
1085                                                 vsocket->socket_fd) == -1) {
1086                                         pthread_mutex_unlock(&vhost_user.mutex);
1087                                         goto again;
1088                                 }
1089
1090                                 close(vsocket->socket_fd);
1091                                 unlink(path);
1092                         } else if (vsocket->reconnect) {
1093                                 vhost_user_remove_reconnect(vsocket);
1094                         }
1095
1096                         pthread_mutex_destroy(&vsocket->conn_mutex);
1097                         vhost_user_socket_mem_free(vsocket);
1098
1099                         count = --vhost_user.vsocket_cnt;
1100                         vhost_user.vsockets[i] = vhost_user.vsockets[count];
1101                         vhost_user.vsockets[count] = NULL;
1102                         pthread_mutex_unlock(&vhost_user.mutex);
1103
1104                         return 0;
1105                 }
1106         }
1107         pthread_mutex_unlock(&vhost_user.mutex);
1108
1109         return -1;
1110 }
1111
1112 /*
1113  * Register ops so that we can add/remove device to data core.
1114  */
1115 int
1116 rte_vhost_driver_callback_register(const char *path,
1117         struct vhost_device_ops const * const ops)
1118 {
1119         struct vhost_user_socket *vsocket;
1120
1121         pthread_mutex_lock(&vhost_user.mutex);
1122         vsocket = find_vhost_user_socket(path);
1123         if (vsocket)
1124                 vsocket->notify_ops = ops;
1125         pthread_mutex_unlock(&vhost_user.mutex);
1126
1127         return vsocket ? 0 : -1;
1128 }
1129
1130 struct vhost_device_ops const *
1131 vhost_driver_callback_get(const char *path)
1132 {
1133         struct vhost_user_socket *vsocket;
1134
1135         pthread_mutex_lock(&vhost_user.mutex);
1136         vsocket = find_vhost_user_socket(path);
1137         pthread_mutex_unlock(&vhost_user.mutex);
1138
1139         return vsocket ? vsocket->notify_ops : NULL;
1140 }
1141
1142 int
1143 rte_vhost_driver_start(const char *path)
1144 {
1145         struct vhost_user_socket *vsocket;
1146         static pthread_t fdset_tid;
1147
1148         pthread_mutex_lock(&vhost_user.mutex);
1149         vsocket = find_vhost_user_socket(path);
1150         pthread_mutex_unlock(&vhost_user.mutex);
1151
1152         if (!vsocket)
1153                 return -1;
1154
1155         if (fdset_tid == 0) {
1156                 /**
1157                  * create a pipe which will be waited by poll and notified to
1158                  * rebuild the wait list of poll.
1159                  */
1160                 if (fdset_pipe_init(&vhost_user.fdset) < 0) {
1161                         VHOST_LOG_CONFIG(ERR,
1162                                 "failed to create pipe for vhost fdset\n");
1163                         return -1;
1164                 }
1165
1166                 int ret = rte_ctrl_thread_create(&fdset_tid,
1167                         "vhost-events", NULL, fdset_event_dispatch,
1168                         &vhost_user.fdset);
1169                 if (ret != 0) {
1170                         VHOST_LOG_CONFIG(ERR,
1171                                 "failed to create fdset handling thread");
1172
1173                         fdset_pipe_uninit(&vhost_user.fdset);
1174                         return -1;
1175                 }
1176         }
1177
1178         if (vsocket->is_server)
1179                 return vhost_user_start_server(vsocket);
1180         else
1181                 return vhost_user_start_client(vsocket);
1182 }