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